bob wrote:
>
> Yes, it's thread-safe because setting a boolean is an atomic operation.
>
That is fallacious.

Setting a boolean may be atomic, but that has nothing to do with thread 
safety.

Thread safety in Java is determined by "happens-before" relationships and 
related concepts.
http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.4
specifically 
http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.4.5

Please do not promulgate false information.

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.


Per the referenced link, you can see that 'thread.start()' is a 
synchronization edge. 
So the use in 'surfaceCreated()' is thread safe. However, the 
synchronization edge 
for 'join()' is after it returns , which to my eye means that 
'surfaceDestroyed()' is not. 

> 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 

Nope. 

Being a "simple boolean variable" doesn't make it thread safe.

> and is only updated from one of the two threads involved.  If mRun was a 
more complext 

Nope. Being *accessed* is what counts, not just updated.

> 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.
 
As it likely isn't now, unless my analysis is incorrect.

-- 
Lew

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