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 What should I use instead of Thread.stop? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "What should I use instead of Thread.stop?" Watch "What should I use instead of Thread.stop?" New topic
Author

What should I use instead of Thread.stop?

Wagner Danda Da Silva Filho
Ranch Hand

Joined: Mar 21, 2003
Posts: 80
From "Why Are Thread.stop, Thread.suspend, Thread.resume and Runtime.runFinalizersOnExit Deprecated? (http://java.sun.com/j2se/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html)"

What should I use instead of Thread.stop?
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. (This is the approach that JavaSoft's Tutorial has always recommended.) To ensure prompt communication of the stop-request, the variable must be volatile (or access to the variable must be synchronized).

What does mean this part in bold (about volatile variable)?


SCJP, SCWCD
Ryan Kurtz
Greenhorn

Joined: Feb 26, 2004
Posts: 1
If your class contains a member variable that is modified asynchronously by concurrently running threads, you can use Java's volatile keyword to notify the Java runtime system of this.
At this time, the Java runtime system ignores the volatile marker. However, future releases of the Java runtime system will use this information to ensure that the variable is loaded from memory before each use, and stored to memory after each use to ensure that the value of the variable is consistent and coherent within each thread.
The following variable declaration is an example of how to declare that a variable can be modified asynchronously by concurrent threads.
class VolatileExample {
volatile int counter;
. . .
}
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Coherent maybe, but not consistent.
The bold statement is meaning to indicate that you must use some technique to ensure one thread realizes when a certain variable has been changed by another thread.
In essense you want the runnign thread to always check a variable
if(run == true)
then you have another thread that will set run = false, when you want the thread to stop.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: What should I use instead of Thread.stop?
 
Similar Threads
Stopping a method
threads and display
Regarding finnaly
How to kill a thread????
Question thread