• Post Reply Bookmark Topic Watch Topic
  • New Topic

stopping a thread execution

 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!