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/package-descr.html

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