BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 12-02-2009, 01:50 AM   #1 (permalink)
Knows Where the Search Button Is
 
Join Date: Jul 2008
Location: India
Model: 9000
Carrier: AirTel
Posts: 29
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default How to destroy a thread in execution ?

Please Login to Remove!

Hi,
I am having multiple threads in my application. I want to stop one thread when next thread is starting.

Code:
private void stopDataDownloadThread(Thread dataDownloadThread )
    {
    	if(dataDownloadThread != null && dataDownloadThread.isAlive())
        {
            dataDownloadThread.interrupt();
            
            while(dataDownloadThread.isAlive())
            {
                try
                {
					Thread.sleep(100);
                }
                catch(Exception ex)
                {
                	UtilityClass.printDebugString("DEBUG:Exception occured while stopping the previous thread." + ex);
                }
            }
        }
        dataDownloadThread = null;
    }
this is the way i am trying to stop the thread in execution. But it is not destroying the thread. Is there any other way ? In Java there is option to destroy, but in blackberry ? anyone ?
__________________
Praveen K
Offline  
Old 12-02-2009, 05:55 AM   #2 (permalink)
Thumbs Must Hurt
 
Join Date: Jun 2008
Model: 9000
Carrier: Airtel
Posts: 81
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

try this

private void stopDataDownloadThread(Thread dataDownloadThread )
{
boolean quite = false;
if(dataDownloadThread != null && dataDownloadThread.isAlive())
{
dataDownloadThread.interrupt();

while(!quite)
{
try
{
Thread.sleep(100);
}
catch(Exception ex)
{
UtilityClass.printDebugString("DEBUG:Exception occured while stopping the previous thread." + ex);
}
}
}
quite = true;
}
__________________
blog.vimviv.com
Offline  
Old 12-02-2009, 09:12 AM   #3 (permalink)
Knows Where the Search Button Is
 
Join Date: Jul 2008
Location: India
Model: 9000
Carrier: AirTel
Posts: 29
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi , thanks for your reply,
But this one also not worked for me. When i am invoking a thread second time i am geting IllegalThreadStateException.Which means the thread is still alive and , again i am trying to start it. I want to destroy the thread and start again. Any helps ?
__________________
Praveen K
Offline  
Old 12-02-2009, 10:43 AM   #4 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

I think the correct steps are:

1. Set a variable that is queried by the thread run() loop (like isDone). The run() loop should always exit when this value is set.

2. call thread.interrupt() to break the thread from any current sleep()

3. call thread.join() to wait for the thread to die.

You can also use thread.isActive() to check and see if the thread is really dead.

Now, having said all of that, I never re-use a thread. I kill the old one, set the reference to null (so that it can be garbage collected), and start a new thread.
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.