Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

methods which stop a thread?

 
shreya prabhu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone please tell me which are the methods that can stop a thread from executing?
wait()
does yield stop it from executing?are there any other methods?
thanks in advance
 
sushil bhogale
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are different states for thread
1. New
2. Runnable
3. Running
4. Dead
5. Blocked / Waiting.

A thread is in New state, when it is just instantiated. to put into Runnable state, start() method should be invoked. Now thread goes into its Running mode, when JVM decides it to run (again JVM specific). Now such a runnign thread can be moved from Running to the Runnable state by following ways -
1. Sleep - for specific time
2. Wait - on the mercy of other threads to notify
3. Blcoked for any I/O Expception

Once the run() method gets over, a thread can not be made Live again. It would throw an exception.
 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

yield() is not guaranteed.
The static method sleep(long millis) is guaranteed to sleep the thread for at least the amount of time you specify in millis.

By at least, it's meant that when the thread wakes up again, it may not be running again directly, because when it wakes up, it goes to the runnable state again.

There's also the join() method, which is guaranteed, and it causes the current running thread to stop until the thread in which it joins finishes executing.

join() also has an overloaded version, that takes an amount of time, so in this case the current thread will wait until the thread it joins finished OR until the amount of time specified.

Best of luck ...
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop would not be the right word for it because there's a deprecated stop() method in the Thread class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic