alexk-il wrote:
> Hi,
> 
> I'm really enjoying Mark's "Beginning Android" - everything is clear
> and straight to the point.
> 
> I wonder why in the Threading example the "isRunning" flag wasn't
> declared "volatile". The flag is used to stop a thread from another
> thread.
> 
> In case you have the book (or even wrote it :) ) - it's on page 143.
> In case you don't, the simplified version of the code goes like this:
> 
> public class fooBar extends Activity{
>    ...
>    public void onStart() {
>        super.onStart();
> 
>        // why not volatile?
>        boolean isRunning=false;
> 
>        Thread BG=new Thread(new Runnable() {
>            public void run() {
>               try {
>                   while(isRunning){
>                      // Do something
>                   }
>               }
>               catch (Throwable t) {
>                  // Do something
>              }
>       });
> 
>       isRunning=true;
>       background.start();
>    }
> 
>    public void onStop() {
>       super.onStop();
>       isRunning=false;
>    }
> }
> 
> I know, the code works on android, so what am I mising here?

Hmmmmm...

Usually, I use AtomicBoolean rather than volatile, but I apparently used
neither here. On the whole, booleans are as thread-safe as a datatype
gets, since we're only talking about a bit. That being said, I am being
a bit sloppy there, and should improve matters.

Thanks for pointing it out!

BTW, you're welcome to use the [cw-android] Google Group for questions
on _Beginning Android_:

http://groups.google.com/group/cw-android

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

Android Training in US: 8-12 February 2010: http://bignerdranch.com

-- 
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.

NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android

To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en

Reply via email to