Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.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: Wed, 18 Jan 2012 10:12:54 -0800 Organization: A noiseless patient Spider Lines: 31 Message-ID: References: <09848313-2372-4c23-8f52-fa84c612c100@u32g2000yqe.googlegroups.com> <1pi7kea3zdo0b.1ixhuq3p9ybbu$.dlg@40tude.net> <60dddbf9-3686-4824-a918-64a59faba177@a8g2000vba.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 18 Jan 2012 18:12:57 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="XjIWM99mD7Ijfdu600oVPA"; logging-data="20555"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1861ao2X0JrpenagOBlbqwslwASghZjZP0=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0 In-Reply-To: Cancel-Lock: sha1:iI4Rqutbj1ki+V4koaZh9+ZXuHA= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:11470 On 1/18/2012 8:34 AM, raphfrk@gmail.com wrote: > On Jan 18, 3:32 pm, Peter Duniho wrote: >> On Wed, 18 Jan 2012 06:24:54 -0800 (PST), raph...@gmail.com wrote: >>> http://mailinator.blogspot.com/2007/05/readerwriter-in-java-in-nonblo... >>> [...] >> >> It looks correct to me. > > I don't understand why it is ok. Doesn't it suffer from the same > issue as my example? Yeah, it's a trick. Or at least isn't great documentation. To figure it out, you have to read the introduction to the AtomicInteger documentation, which tells you that you need to read the package documentation for "the properties of atomic variables." In the package documentation, it eventually explains that: "get has the memory effects of reading a volatile variable. "set has the memory effects of writing (assigning) a volatile variable. " So you are synchronizing here just as if you were reading and writing to a volatile. I don't think the implementation uses volatile, I think it uses native methods which have the same effect, but you can think of the internal values as using a volatile to hold the atomic values. Voila!