Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What should I use instead of Thread.stop?  RSS feed

 
Wagner Danda Da Silva Filho
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)?
 
Ryan Kurtz
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!