File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Thread.stop() depricated.. alternate?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread.stop() depricated.. alternate??" Watch "Thread.stop() depricated.. alternate??" New topic
Author

Thread.stop() depricated.. alternate??

Sandeep Jindal
Ranch Hand

Joined: Aug 25, 2003
Posts: 180
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


SCJP 5.0
http://sites.google.com/site/duddlutechnologies/home
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
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.
Sandeep Jindal
Ranch Hand

Joined: Aug 25, 2003
Posts: 180
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
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread.stop() depricated.. alternate??
 
Similar Threads
depricated mathod stop(), no alternate??
Stop() in threads
stopping the thread
how to make simple sleep()
Using interupt to stop the Thread