Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: markspace <-@.> Newsgroups: comp.lang.java.programmer Subject: Re: Volatile happens before question Date: Tue, 17 Jan 2012 11:24:03 -0800 Organization: A noiseless patient Spider Lines: 27 Message-ID: References: <09848313-2372-4c23-8f52-fa84c612c100@u32g2000yqe.googlegroups.com> <1e7b4r38kmn2f$.1kwqeem7f8lxb.dlg@40tude.net> <40835b59-f9af-4895-bb47-2f7dcf475e3a@k6g2000vbz.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 17 Jan 2012 19:24:06 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="XjIWM99mD7Ijfdu600oVPA"; logging-data="30638"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ewSSsOaPWv4M4ogKyotVdzFW8k2sHSpU=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0 In-Reply-To: <40835b59-f9af-4895-bb47-2f7dcf475e3a@k6g2000vbz.googlegroups.com> Cancel-Lock: sha1:DD+QlXNmaocRXZ6FU4pZGQeW1gg= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:11422 On 1/17/2012 8:54 AM, raphfrk@gmail.com wrote: > However, if it places a sync actions later than another sync action, > that still doesn't mean that the later one "happens after" the first > one? Specifically, no. It wasn't clear to me that was the question you where asking. (Sorry about that!) Your program was a little hard to read. But the answer is that there is are no semantics for multi-threaded reads and rights if there is no happens-before relationship. What you have there is called a data race. Those are completely broken. The reason is that hardware involved will optimize reads and writes aggressively. (The same problem would occur even if you wrote this code in assembly language. This is not a Java problem, it's a system problem. The JLS actually just tells you how modern CPUs work.) In this case, by creating a data race and reading b before you create the happens-before relationship, Thread 2 could see any value for b, even values that are neither 1 nor 0. It's a random number generator. (And the more complete story is even worse. I'm still looking for some sources to back that up though.)