[android-developers] Re: sub-thread issue.

2009-05-31 Thread TjerkW

I created a game which also requires a gameloop thread.
I communicate with the thread simply by using getters and setters.
But that requires that the other threads has a the new thread as a
member instance.

I use the AsyncTask for loading levels, not for the game loop.

On 31 mei, 06:59, Gavin  wrote:
> Hi,
>
>      I have some questions about threads.
>      Threre is a main thread A, and then it creats a sub-thread B. I
> want to know:
>     1) If main thread A exits or crashs,  thread B can continue
> running?
>     2) if the upper question's anwser is yes, is there a method that A
> can notify B before A exits? How can B get A's state?
>
>     thanks.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---



[android-developers] Re: sub-thread issue.

2009-05-31 Thread Mark Murphy

Gavin wrote:
>  Threre is a main thread A, and then it creats a sub-thread B. I
> want to know:
> 1) If main thread A exits or crashs,  thread B can continue
> running?

If thread A exits, thread B will continue running.

If an exception occurs in thread A, whether thread B continues running
depends on what catches that exception, and whether the process is
terminated as a result. Ideally, you catch your own exceptions, and as
such thread A and thread B will still be running.

> 2) if the upper question's anwser is yes, is there a method that A
> can notify B before A exits? How can B get A's state?

There are many ways to achieve your desired pattern, mostly involving
classes in java.util.concurrent.

For example:

-- You can use an AtomicBoolean to set a flag from thread A that thread
B monitors to know when to shut down

-- You can have thread B pull work off a LinkedBlockingQueue, with a
special "kill" piece of working telling thread B to close up

However, better than all of that is to switch to using Android 1.5's
AsyncTask, if possible, because you get all the benefits of background
threads without having to manage them yourself.

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

Need help for your Android OSS project? http://wiki.andmob.org/hado

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