Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: reading the JLS (17.4.5) Date: Tue, 20 Dec 2011 19:08:01 -0800 (PST) Organization: http://groups.google.com Lines: 37 Message-ID: <17674291.80.1324436881159.JavaMail.geo-discussion-forums@prez5> References: Reply-To: comp.lang.java.programmer@googlegroups.com NNTP-Posting-Host: 2620:0:1000:fd2b:224:d7ff:fe69:5838 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1324437327 2234 127.0.0.1 (21 Dec 2011 03:15:27 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 21 Dec 2011 03:15:27 +0000 (UTC) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=2620:0:1000:fd2b:224:d7ff:fe69:5838; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10924 Andreas Leitgeb wrote: > Given some (simplified) sample code: > class Test { > /*non-vol*/ int n1; > volatile int v1; > > void t1() { > n1 = 1; > v1 = n1; // v1 uses n1 > } > > void t2() { > int r1=v1, r2=r1*n1; // r2 uses r1 > assert ! (r1 == 1 && r2 != 1) : "huh?"; > } > } > Two threads T1 and T2 may at some point run t1() and t2() > respectively, then I should expect, per transitivity of You have to establish a /happens-before/ between the invocations of t1() an t2(). If two threads begin to execute the methods, you cannot guarantee that t1() will /happen-before/ t2(), so the read of 'v1' in the latter could result in the default value. -- Lew > "happens-before" that if r1 == 1, then r2 would *have to* > == 1, too. Is there a happens-before relation between > between setting n1 and reading n1 via write&read on the > volatile v1 and each intra-thread ordering? > > There is a slightly similar example later in the JLS about > a final and a non-final set in a constructor, but that is > different, in that the non-final is set after the final. > I don't know for sure, if the ordering of the assignments > was relevant in that example.