BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 12-02-2009, 01:50 AM   #1
pravipravi
Knows Where the Search Button Is
 
Join Date: Jul 2008
Location: India
Model: 9000
Carrier: AirTel
Posts: 29
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
vivartpandey
Thumbs Must Hurt
 
Join Date: Jun 2008
Model: 9000
Carrier: Airtel
Posts: 81
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
pravipravi
Knows Where the Search Button Is
 
Join Date: Jul 2008
Location: India
Model: 9000
Carrier: AirTel
Posts: 29
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
Dougsg38p
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
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



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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


ZURN WILKINS 1-720A  1

ZURN WILKINS 1-720A 1" SPRINKLER SYSTEM PRESSURE VACUUM BREAKER ASSEMBLY VALVE

$79.75



Orion Motor Tech Vacuum Pump, 4.5 cfm 1/3 hp HVAC Single Stage Vacuum Pump picture

Orion Motor Tech Vacuum Pump, 4.5 cfm 1/3 hp HVAC Single Stage Vacuum Pump

$30.00



4.5 CFM Single-Stage Rotary Vacuum Pump HVAC/Auto AC 4.5CFM 1/3HP 1/4

4.5 CFM Single-Stage Rotary Vacuum Pump HVAC/Auto AC 4.5CFM 1/3HP 1/4"ACME inlet

$29.33



Oilless Diaphragm Vacuum Pump Industrial Oil Free Piston Vacuum Pump 550W picture

Oilless Diaphragm Vacuum Pump Industrial Oil Free Piston Vacuum Pump 550W

$106.40



Single-Stage Vacuum Pump Lithium Battery DC 2.5CFM Vacuum Pump Air Conditioner picture

Single-Stage Vacuum Pump Lithium Battery DC 2.5CFM Vacuum Pump Air Conditioner

$127.30



Dental Vacuum Molding Forming Machine Former Heat Thermoforming Lab Equipment picture

Dental Vacuum Molding Forming Machine Former Heat Thermoforming Lab Equipment

$104.74







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