File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes methods which stop a thread? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "methods which stop a thread?" Watch "methods which stop a thread?" New topic

methods which stop a thread?

shreya prabhu
Ranch Hand

Joined: Feb 10, 2007
Posts: 31
can anyone please tell me which are the methods that can stop a thread from executing?
does yield stop it from executing?are there any other methods?
thanks in advance
sushil bhogale

Joined: Mar 13, 2007
Posts: 8
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

Joined: Mar 08, 2007
Posts: 1585

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 ...

Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
Stop would not be the right word for it because there's a deprecated stop() method in the Thread class.
I agree. Here's the link:
subject: methods which stop a thread?
It's not a secret anymore!