Click to See Complete Forum and Search --> : Ending a Thread
winston2020
April 14th, 2008, 09:17 PM
Let's say I have something like this:
public class UsingThreads {
/**
* @param args
*/
public UsingThreads(){
Thread t = new Thread(new Thread1Class());
t.start();
try{
Thread.sleep(2000);
// WANT TO END THREAD HERE**************
//**************************************
}catch(Exception e){
System.out.println("Error: " + e.getMessage());
}
}
public static void main(String[] args) {
new UsingThreads();
}
class Thread1Class extends Thread{
public void run(){
while(true){
System.out.println("Still Running...");
try{
Thread.sleep(500);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}
}
What could I do to end Thread t where indicated?
sinha.techie
April 15th, 2008, 12:17 AM
t.stop();
Read properly in Java Doc before using that .
-Gaurav
Londbrok
April 15th, 2008, 07:57 AM
Would not advice to use Thread.stop(). You should design your threads so that they can terminate naturally, that is "run out of code to execute". Only use while(true) loop, with no exit logic build in somewhere, when you do need a thread that runs forever. In such case, set the thread as daemon.
Since you have made a class that extends thread, you could give it a boolean, determining the running state. The element that needs to govern when the thread should stop would then set the boolean to false. So youŽd use while(isRunning()) instead of while (true). Remember to synchronize access to the boolean.
When wroking with multiple threads, it is good to check if any of the ExecutorServices provided by Java would service you better than simply launching new Threads. Point of the crux is, that in many many cases Threads dont make better, or faster, applications. They are very handy quite often, but should be used with caution.
sinha.techie
April 15th, 2008, 10:55 AM
Would not advice to use Thread.stop(). You should design your threads so that they can terminate naturally, that is "run out of code to execute". Only use while(true) loop, with no exit logic build in somewhere, when you do need a thread that runs forever. In such case, set the thread as daemon.
Since you have made a class that extends thread, you could give it a boolean, determining the running state. The element that needs to govern when the thread should stop would then set the boolean to false. So youŽd use while(isRunning()) instead of while (true). Remember to synchronize access to the boolean.
When wroking with multiple threads, it is good to check if any of the ExecutorServices provided by Java would service you better than simply launching new Threads. Point of the crux is, that in many many cases Threads dont make better, or faster, applications. They are very handy quite often, but should be used with caution.
true , that why this method is deprecated.
and that's why I told to Read Properly in Java Doc ,
but the thread he have written can be stopped by using stop method .
keang
April 15th, 2008, 11:53 AM
and that's why I told to Read Properly in Java DocI think the point is you shouldn't have suggested using it in the first placebut the thread he have written can be stopped by using stop method .The code posted may well work safely with the stop() method but you don't know if this is the entirety of the code and/or what modifcations may be made to it in the future. Deprecated methods should not be used and in particluar the Thread classes stop(), suspend() and resume() methods should never be used under any circumstances, they are totally broken and unsafe.
winston2020 read Londbrok's post and do as he/she suggested.
winston2020
April 15th, 2008, 03:30 PM
I actually have been using threads a lot lately, and I almost always use a boolean in a while loop to determine if the thread should end.
The reason I'm asking is because I'm starting to get into network stuff, and for example, I spawn a thread to use as the server (because invoking the accept() method on SocketServer causes the program to freeze until a connection is made). But what if I want to end the program? If I just stop the program with System.exit(0), or in a GUI click the x button, the program will stop, but the server is not ended cleanly, which leaves the port inaccessible.
So, I want to know how I can end just the thread, and do cleanups.
dlorde
April 15th, 2008, 04:34 PM
The Java Docs suggest two alternatives to the stop() method - use a flag variable, or the Thread.interrupt() method.
There is nothing so useless as doing efficiently that which should not be done at all...
P. Drucker
keang
April 15th, 2008, 06:12 PM
But what if I want to end the program? If I just stop the program with System.exit(0), or in a GUI click the x button, the program will stop, but the server is not ended cleanly, which leaves the port inaccessible.If you are stopping this thread in response to a program shut down then you probably want to use the interrupt() mechanism as it will be more responsive. If the shutdown is program generated, shutting down the server thread should be fairly easy, for a user generated shutdown you could add a WindowAdapter which will receive a window closing notification and so again you can relatively easily shut down the server thread. The final scenario is if the user kills the application eg by typing Ctrl C at the command prompt. This possibly can be handled by adding a shutdown hook using Runtime.addShutDownHook(..) and shutting the server thread down at this point. I've never used shut down hooks in java so I've no idea what is involved but I suspect that dealing with a system that is shutting down all around you may throw up some challenges.
winston2020
April 15th, 2008, 07:18 PM
Thanks, I'll try to figure that out. :)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.