Cyril... thanks... I implemented that yesterday, but I think I came up with
something even more robust while I was testing it, so I thought I'd share
it.
My thread downloads images from my server, using http, and sends them back
to my UI thread using messages. Sometimes that can take some time, so what I
did was make the Handler a public volatile in the Bitmap thread, wrapped the
message call in a try/except and put in a check for a null Handler around
the call. Then in my UI thread I simply set the Handler in the background
Thread to null, and called the stop method.
That way, I don't block the UI thread at all, my background thread can
continue without causing problems, and it will quit when it finishes the
next operation.
My UI thread now looks like this :
protected void onDestroy()
{
if (bitmapThread != null)
{
bitmapThread.handler = null;
bitmapThread.StopThread();
}
super.onDestroy();
}
And my background Thread class looks like this :
class StartBitmapThread
{
private volatile boolean stopRequested = false;
public volatile Handler handler;
Thread backgroundThread;
public boolean StopThread()
{
if (backgroundThread.isAlive())
{
stopRequested = true;
return false;
}
else
return true;
}
public StartBitmapThread(Handler handler,
ArrayList<ArrayList<String>> bitmapsToDownload)
{
this.handler = handler;
this.BitmapsToDownload = bitmapsToDownload;
StartThread();
}
private void StartThread()
{
backgroundThread = new Thread(new Runnable()
}
I'm not 100% convinced this solves every problem (I'm the paranoid/anal
type), but it does seem to work very well.
I would appreciate anyone pointing out any problems with this implementation
:)
Sincerely,
Brad Gies
-----------------------------------------------------------------
Brad Gies
27415 Greenfield Rd, # 2,
Southfield, MI, USA
48076
www.bgies.com www.truckerphone.com
www.EDI-Easy.com www.pricebunny.com
-----------------------------------------------------------------
Moderation in everything, including abstinence
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Cyril Jaquier
Sent: Monday, January 05, 2009 11:25 AM
To: [email protected]
Subject: [android-developers] Re: Stopping a Thread
Hi Brad,
> Can anyone tell me how to safely stop a thread?
>
You can't stop a thread but you can let it die. Just return from the
Thread#run method. Most of the time you will have something like:
public volatile boolean shutdownRequested = false;
public void run() {
while (!shutdownRequested) {
// ... do something ...
}
}
Just set the shutdownRequested variable to true to quit the loop and
stop the thread.
Regards,
Cyril
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---