dcsimg
How to kill a Thread???
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   fatwiggens_X
Posted On:   Friday, October 26, 2001 08:04 AM

How do I kill a Thread if the code hangs within the run() ? myThread = new Thread() { public void run() { while(flag) { System.out.println("doh"); } } }; myThread.start(); //these next two lines of course work but stop() is deprecated Thread.sleep(5000); //myThread.stop(); So my question is 'pretend' the System.out.println hangs and never continues so you never make it to the bottom bracket of the while loop . Therefore you never have r   More>>

How do I kill a Thread if the code hangs within the run() ?





myThread = new Thread()

{

public void run()

{

while(flag)

{

System.out.println("doh");

}

}

};

myThread.start();

//these next two lines of course work but stop() is deprecated

Thread.sleep(5000);

//myThread.stop();




So my question is 'pretend' the System.out.println hangs and never continues so you never make it to the bottom bracket of the while loop . Therefore you never have reference (can't set Thread=null) so you need a direct handle to the Thread (like the stop() method). So how do I kill the Thread without using the stop()

Thanks!!!

   <<Less

Re: How to kill a Thread???

Posted By:   fatwiggens_X  
Posted On:   Wednesday, October 31, 2001 11:44 AM

Of course stop() is deprecated... that is what my commented code says. And if the thread ran its course properly then of course I could use the flag... The problem is, what if the process in the thread is a run away process. The faq example tells me to get a handle on the running process within the thread. That is not an option in my case. I need a handle to the thread itself. My only option that seems available is to make the same OS stop0() call that Thread.stop() does. I just love how sun deprecates a function but give no viable alternatives.

Re: How to kill a Thread???

Posted By:   Alex_Chaffee  
Posted On:   Monday, October 29, 2001 05:46 AM

Re: How to kill a Thread???

Posted By:   Michael_Lueders  
Posted On:   Sunday, October 28, 2001 09:44 AM

you can try myThread.interrupt(), but interrupting i/o is flaky and may or may not work, depending on your os. jdk 1.4 is supposed to have support for non-blocking i/o, but, until then, there's no clean way (i know of) to do it.

Re: How to kill a Thread???

Posted By:   Luigi_Viggiano  
Posted On:   Friday, October 26, 2001 04:21 PM

The 'stop' is deprecated, as a thread should never be killed suddenly: good programming says that the thread shoud finish normally, setting the 'flag' of your code to false, and avoiding hangs. If your flag is defined as a member instance of your class you can also call it 'stop' an making accessory methods (get/set) to pilot the running thread.
About | Sitemap | Contact