Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #25777
| From | v_borchert@despammed.com (Volker Borchert) |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: boolean and thread safety |
| Date | 2011-02-12 06:21 +0000 |
| Organization | Private site at Eddersheim, Germany |
| Message-ID | <ij58u4$h9f$2@Gaia.teknon.de> (permalink) |
| References | <e9ded49c-dfed-4642-83cb-f4b183b6358c@i40g2000yqh.googlegroups.com> |
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
Re: boolean and thread safety v_borchert@despammed.com (Volker Borchert) - 2011-02-12 06:21 +0000
csiph-web