aspose file tools
The moose likes Threads and Synchronization and the fly likes stopping a thread execution Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "stopping a thread execution" Watch "stopping a thread execution" New topic
Author

stopping a thread execution

amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231
Can we stop a thread from executing by:
1) calling the thread's stop() method?
2) calling System.exit(0).
What is the use of yield?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

stop() will stop a thread, but it's potentially dangerous, as it can leave some data in an indeterminate state (for instance, it can leave a double value half-modified.) System.exit() will stop all threads and exit the application; kind of drastic.
The best way to stop a thread is for the thread to expect to be stopped; it can check a flag or condition periodically, and if that condition is changed, then it can stop.
yield() is not an especially useful method these days. It asks the JVM to allow another thread of the same or higher priority to run, if there are any runnable threads of the same or higher priority. The earliest JVMs didn't use any kind of time-slicing to schedule threads; modern ones generally do, so yield() won't have much of an effect.


[Jess in Action][AskingGoodQuestions]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi all
I don't know whether this answers your question or I am totally correct. But i guess using something like

This way I guess you wouldn't have to actually worry about using the stop() method.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: stopping a thread execution
 
Similar Threads
Dynamically terminate code which my program can not access?
yield();
Serving XML data on web
notify() question..
Gaunt's moan of the week :)