aspose file tools
The moose likes Beginning Java and the fly likes Stopping a Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Stopping a Thread" Watch "Stopping a Thread" New topic
Author

Stopping a Thread

Sharada Kumaran
Greenhorn

Joined: May 23, 2001
Posts: 29
I need to stop a thread when I exit an application. I use myThread.stop(), but this is a deprecated method. Which is the correct way to terminate a thread?
The need arises because I am caling an application from another. On exiting the called application, when control returns to the first one, the thread is still running.
thanks,


Sharon
L Goundalkar
Ranch Hand

Joined: Jul 05, 2001
Posts: 395
Most uses of stop should be replaced by code that simply modifies some variable to indicate that the target thread should stop running. The target thread should check this variable regularly, and return from its run method in an orderly fashion if the variable indicates that it is to stop running. To ensure prompt communication of the stop-request, the variable must be volatile (or access to the variable must be synchronized).
For example, suppose your applet contains the following start, stop and run methods:
private Thread blinker;
public void start() {
blinker = new Thread(this);
blinker.start();
}
public void stop() {
blinker.stop(); // UNSAFE!
}
public void run() {
Thread thisThread = Thread.currentThread();
while (true) {
try {
thisThread.sleep(interval);
} catch (InterruptedException e){
}
repaint();
}
}
You can avoid the use of Thread.stop by replacing the applet's stop and run methods with:
private volatile Thread blinker;
public void stop() {
blinker = null;
}
public void run() {
Thread thisThread = Thread.currentThread();
while (blinker == thisThread) {
try {
thisThread.sleep(interval);
} catch (InterruptedException e){
}
repaint();
}
}
Thanks.


------------------
L Goundalkar
lggoundalkar@hotmail.com
Sun Certified Programmer for Java 2 Platform


<b>L G Goundalkar</b><br /> <a href="mailto:lggoundalkar@yahoo.com" rel="nofollow">lggoundalkar@yahoo.com</a> <br />Sun Certified Programmer for Java 2 Platform.<br />Sun Certified Web Component Developer for J2EE.
Sharada Kumaran
Greenhorn

Joined: May 23, 2001
Posts: 29
Hey, thats a clean solution. I got the idea, but don't you think that I don't need to override Thread.stop
Is there a problem with this -
public void windowClosing(WindowEvent e){
blinker = null;
System.exit(0);
}
public void run{
while (blinker == myThread)
{
repaint();
}
I ask this because something else I read about setting the reference to null, had me confused.
thanks for your neat solution anyway!
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
The reason that Thread.stop is deprecated is that it can corrupt memory. That's why everyone says to stay away from it.
Stick with code that has a thread check itself to see if it needs to stop executing. That will be your safest bet.
Regards

------------------
Brian Hoff
Sun Certified Programmer for the Java� 2 Platform
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Stopping a Thread
 
Similar Threads
How to distingish between user thread and daemon thread ?
Need to compare date time in GMT
How to kill/stop a Thread
Starting a thread without calling from main
how to maintain single transaction for multiple users