Hi,

Are the threads using synchronized blocks of code or methods when setting / 
getting the common variable, are they using volatile for the boolean ? If 
not thread safety is questionable. It is also a question of visibility as 
well as safety, particularly when running on multi core cpu devices.  

This is not specific to Android but to Java in general, an old resource 
that might be helpful - 
http://www.ibm.com/developerworks/java/tutorials/j-threads/j-threads-pdf.pdf

Regards

On Monday, September 17, 2012 2:36:09 AM UTC+10, sdb wrote:
>
> I'm new to android development and have been studying the Lunar Lander 
> example included in the SDK to get a better understanding of how the 
> SurfaceView works.  I noticed that the mRun member of the LunarThread class 
> is updated by the setRunnable() method of the LunarThread class which is 
> called from the LunarView class within the surfaceCreated() and 
> surfaceDestroyed() methods.
>
> It seems to me that mRun is being updated by the main thread for the 
> SurfaceView, but is being repeatedly read in a tight loop in LunarThread. 
>  My question is: is this use of mRun thread safe and if so, why/how?
>
> I'm not sure, but maybe it is thread safe because mRun is a simple boolean 
> variable and is only updated from one of the two threads involved.  If mRun 
> was a more complext object and/or it was being updated by different by both 
> threads,then perhaps this example would not be thread safe without the use 
> of some form of synchronization mechanism.
>
>

-- 
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