Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #25777

Re: boolean and thread safety

Path csiph.com!eeepc.pasdenom.info!news.pasdenom.info!news.dougwise.org!news-transit.tcx.org.uk!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From v_borchert@despammed.com (Volker Borchert)
Newsgroups comp.lang.java.programmer
Subject Re: boolean and thread safety
Date 12 Feb 2011 06:21:56 GMT
Organization Private site at Eddersheim, Germany
Lines 44
Distribution world
Message-ID <ij58u4$h9f$2@Gaia.teknon.de> (permalink)
References <e9ded49c-dfed-4642-83cb-f4b183b6358c@i40g2000yqh.googlegroups.com>
X-Trace individual.net 86kAKGxgjxdHEieSFB+fbwAjK8RSY7UXZ9qVZXLPAFu+ZxV3iW
Cancel-Lock sha1:MNRGsrLZaSi503S1yEbtrm0rCGg=
Xref csiph.com comp.lang.java.programmer:25777

Show key headers only | View raw


raphfrk@gmail.com wrote:
> Are booleans inherently thread safe?
> 
> I am thinking of something like
> 
> public class AsyncBoolean {
> 
>     boolean value = false;
> 
>     public AsyncBoolean(boolean initValue) {
>         value = initValue;
>     }
> 
>     public boolean getValue() {
>         return value;
>     }
> 
>     public void setValue(boolean value) {
>         value = newValue;
>     }
> 
> }
> 
> Assuming I define the variable using
> 
> final AsyncBoolean varName = new AsyncBoolean(true);
> 
> does this in effect auto-sync, since you can't partially read a byte
> and even if you could it is a boolean anyway?
> 
> Since it is declared as final, the reference can't change, so I could
> then call varName.setValue(someValue) and varName.getValue() from
> threads without the need for syncing.

Others have pointed out ad nauseam that while setValue() is indeed
atomic, the final only guarantees visibility of the varName assignment,
not of any changes to the object pointed to thereby.

Why don't you simply use AtomicBoolean?

-- 

"I'm a doctor, not a mechanic." Dr Leonard McCoy <mccoy@ncc1701.starfleet.fed>
"I'm a mechanic, not a doctor." Volker Borchert  <v_borchert@despammed.com>

Back to comp.lang.java.programmer | Previous | Next | Find similar


Thread

Re: boolean and thread safety v_borchert@despammed.com (Volker Borchert) - 2011-02-12 06:21 +0000

csiph-web