I would think that if the child thread does not modify the flag, just
gets the flags value, you do not need to synchronize the use of this
flag, assuming your child thread is a bit relaxed about the
possibility of miss the if(flag) call once on the loop. But, if the
child and parent could modify the flag state you should sync the set
and get calls...

-Moto

On Oct 18, 5:50 am, Daniel Drozdzewski <[email protected]>
wrote:
> On Mon, Oct 18, 2010 at 10:21 AM, xeagle <[email protected]> wrote:
> > Hi,
>
> > I use a boolean variable "flag" both in main and child thread. This
> > "flag" is used to notify child thread to exit. I only use simple
> > operation, e.g., "flag=false", "if (flag) {}". As I know, in java,
> > "get and set a boolean variable" is atomic. So "synchronized" is not
> > needed for variable "flag", declaring "flag" as "volatile" is enough.
>
> > Is this still true in Android?
>
> It is. Android follows JLS' Memory Model, hence volatile will behave
> exactly as it does in 'normal' JVMs.
> Please mind that in the second statement you mention "if (flag) {}",
> you should not modify the flag beyond the if part of the statement.
>
> There are plenty helpers in *java.util.concurrent.atomic* that will
> help you achieve further atomicity without the use of locks and perils
> of *volatile* 
> :http://developer.android.com/reference/java/util/concurrent/atomic/pa...
>
> Daniel
>
> --
> Daniel Drozdzewski

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to