Forums Register Login

Thread.stop() depricated.. alternate??

+Pie Number of slices to send: Send
Dear All,
I know that Thread.stop() is depricated because of deadlick problems. And the alternate to this is givenm normally as
public void run(){
while(isStop==false){
//do blah-blah
}
}
public void stopThread(){
isStop=false;
}

But what i want is to stop the thread immidealty when i call the stop method. e.g my run is
public void run(){
//do blah 1
//do blah 2
//do blah 3
//do blah 4
}
So should i check if stop isStop=false after every blah.. or there is some alternate? If answer is no, then Sun is doin gr8 injustice with me cause my run method is too large containing a lot of sleeps and lot of method calls.

Regards
Sandeep Jindal
+Pie Number of slices to send: Send
The stop() method is deprecated because it will kill a thread dead in its tracks -- mid-IO operation, mid assignment to a non-atomic variable type, mid-anything. That's just plain dangerous.
If you really want the stop a thread immediately, the only way to do it safely is to check regularly to see if a stop has been requested. Normally you can restrict the number of places where such a check is necessary to a reasonable number if you just allow things that can happen quickly to just happen.
But the moral is, yes you have to manage stopping manually.
+Pie Number of slices to send: Send
Dear David,

But the moral is, yes you have to manage stopping manually.


Thanks for your answer. Your answer confirms my doubt. And i m working this way only. Actually my problem was that my run() method was very large and there were so many lines of code in that. Now just to get the Stop() kind of implementation(to stop the thread at any time as soon as stop() is called), i have to add to many lines extra. Well, when u dont have alternate u have to do that way only. Actaully I posted this question in another forum also, and i got the same answer from Mr Joe.
But David, i dont know much bout interrupt() method of thread. Can this method help me any way? and what is that for?
Thnaks and regards
Sandeep Jindal
+Pie Number of slices to send: Send
interrupt() is most useful for cancelling blocking operations (threads waiting, synchronous IO, etc.). For example, consider this thread:

This might be reading over a socket, for instance. So readline might get the first few characters of a line....and then nothing else. It just hangs waiting for an endline character. You can call Thread.interrupt() on the thread if you notice (from another thread) that it's hanging. That will cause an InterruptedIOException to be thrown by the readLine() method and it will no longer hang.
If you're not stuck in a blocking API call though, interrupt() just sets a flag so that you can call interrupted() and it returns true. Theoretically, then, you could use interrupt() and interrupted() as the stop flag, however there are different cases where a variable is set, an exception is thrown, or both (look at the javadocs).
Would you like to try a free sample? Today we are featuring tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1281 times.
Similar Threads
Stop() in threads
how to make simple sleep()
depricated mathod stop(), no alternate??
Using interupt to stop the Thread
stopping the thread
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:03:02.