4.2 Thread StatesHomepage « Java6 Certification « 4.2 Thread States
In this lesson we investigate the different states in which a thread can exist, and identify ways in which a thread can transition from one state to another.
Lets take a look at the points outlined at the Oracle Website for this part of the certification.
- Section 4: Concurrency
- Recognize the states in which a thread can exist, and identify ways in which a thread can transition from one state to another.
Different Thread States
Top
Before we look at some of the methods of the Thread
class that alter state, it's worth spending a little time talking about the differing states a thread can exist in and how a
thread can transition between the varying states. To make understanding easier we will use a slideshow as we go through the life of a thread; just press the button below to step through each slide.

In this slide we have created a new Thread
object, which is just like any other object.
Interrupting A Thread
Top
The user threads we create don't die with the main()
method and even daemon threads continue processing until their run()
method ends. The overridden
run()
methods in our lesson examples showed this by displaying thread output after the main()
method ended. We can interrupt a thread using the interrupt()
method.
The interrupt()
method doesn't actually stop a thread from running, but sets a flag in the thread that indicates that an interrupt has been requested. The flag must be checked
in the run()
method for its effect to have any impact.
See the interrupt() method for an example, where we use the sleep()
method, in which we have to specify a try
block
that checks to see if this thread has been interrupted and then throws the InterruptedException
for this thread.
The yield()
Method
Top
The static yield()
method puts the currently running thread back to a runnable state to allow other threads of the same priority a chance to run. Although this sounds
a great way to use thread prioritisation and allow equal usage of the CPU in reality there is no guarantee that this will actually happen. What we can guarantee is that the yield()
method
puts the currently running thread back to a runnable state. After this happens which thread gets chosen by the scheduler is out of our hands and it is very possible that the
yielding thread is selected again and goes straight back to executing. Lets have a look at how we can use the yield()
method:
See the yield() method for an example.
Connecting Threads
Top
We have limited control over the order in which threads are run, but we do have some control. We can cease execution of a thread for a specified amount of time using the sleep()
method. We
can also use the yield()
method, to cease execution of a thread up to a specified amount of time and there are also options to prioritize threads as discussed in the
Concurrency - Thread Priorities lesson and how to lock threads as discussed in the Concurrency - Synchronization lesson.
The join()
Method
Top
There is another option available to us, the join()
method of the Thread
class which allows us to connect the current thread to a named thread, which is very handy
when we want to guarantee that one thread finishes before another.
See the join() method for an example, where we connect the current thread to a named thread.
Related Java6 Tutorials
Concurrency - Thread Basics
Concurrency - The Runnable
Interface
Concurrency - Thread Priorities
Concurrency - Synchronization