Path: csiph.com!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: Strange Socket problem Date: Sat, 03 Mar 2012 23:21:19 -0800 Organization: albasani.net Lines: 32 Message-ID: References: <4f512b27$0$26687$65785112@news.neostrada.pl> <4f51f338$0$1256$65785112@news.neostrada.pl> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net mSL7V3oFDagzI8Cq6VQFAGF+Bf86bP3Vlj0+J0InA7CITTbNgLe06QsroNvC5vSwn261JBYoY6VMNQt+4J9q/cJYlLHm4cosHgeZCNZIOaC+iJ93KKstZoIAbK2O/Dll NNTP-Posting-Date: Sun, 4 Mar 2012 07:21:16 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="qaPGQeKqldwEbWDtRlmylJSbu0vtP4DlhDRJfnB7Sji3a3ZTA0L2wKHag3+6Wqwjj+9JhGKY72y9Tat7ZlBQodOo3LJx36mx0DIQ+8YW+eAwX6m/6wSY0TmZLN4diZSL"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2 In-Reply-To: <4f51f338$0$1256$65785112@news.neostrada.pl> Cancel-Lock: sha1:1yA0406Z7lHWlaLUUEer57ugp1Y= Xref: csiph.com comp.lang.java.programmer:12654 x wrote: > But, the volatile keyword does not provide any kind of access control. It just > tells the JVM "hey, if this variable is changed by thread A, the changed value > must be visible for thread B, so don't cache it too aggressively'. It does more than that. All writes from thread A prior to the write to the volatile variable are visible from thread B after the latter reads that variable. However I agree that multiple views of the same volatile variable introduce danger, as in the idiom if (volaVar != null) { volarVar.doSomething(); } because there's a threat window between the conditional check for non-'null' and the dereference to the method call where another thread could set the variable to a different value. There's also a threat when you use two volatile variables to control a code block: if (volaVar1.equals(SOME_VALUE) && volaVar2.equals(DIFFERENT_VALUE)) ... because 'volaVar1' can change by the time you test the second clause. -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg