The Life Cycle of a Thread

   

A thread can undergo one of any five states during the complete life cycle (see Figure 11.4). The five states are

Figure 11.4. The life cycle of a thread can illustrate all five states.

graphics/11fig04.gif

  • New

  • Runnable

  • Running

  • Not Runnable

  • Dead

When the SimpleThread class was instantiated in the previous section, the thread was in the New state. No system resources are allocated to a thread in the New state. The only method that can be called on a new thread is the start method; otherwise an IllegalThreadStateException will be thrown.

When the start method is called on a new thread, the thread enters the Runnable state and is considered to be running. This does not mean that the thread is actually executing instructions. This is where thread scheduling comes in. After start is called, the new thread is set to the Runnable state and is scheduled for running. When the scheduler decides it is time for the new thread to get some CPU time, it gives control over to the thread. After the scheduler decides the thread has had enough of the CPU or another thread with a higher priority comes along, the scheduler causes the thread to yield. You will learn about priorities later in this chapter. Keep in mind that this scheduling is dependant on many factors, such as operating system, CPU manufacturer, and which Java VM is being used. If the operating system does not having a thread-scheduling algorithm, the Java VM will use its own implementation to schedule threads instead of the operating system.

There are three conditions that cause a thread not to be runnable:

  • The sleep method is called.

  • The thread called the wait condition.

  • The thread is waiting on I/O.

After sleep is called, the thread is considered not Runnable, even if there are no other threads available. After the thread comes out of the sleep, the thread is put back into the Runnable state and ready for scheduling to give it to the CPU.

If one thread needs to communicate or work with another thread, it can call the wait method and wait for the other thread to catch up. After the second thread is ready, it can notify the first thread through the notify method. You will learn more about this when thread synchronization is explained later in this chapter.

The final condition when a thread is considered not Runnable is if a thread is waiting for I/O. In this case, it's up to the thread to finish the I/O and get back into the Runnable state.

Notice in the SimpleThread example from the previous section that nothing special was done in the run method to terminate the thread. It's normally up to the thread to terminate itself. It does this by letting the run method finish. In Listing 11.4,the thread was put to sleep and then a few statements were printed out when it woke up. After that, the thread was free to exit the run method, which actually stopped that thread. It's very common to have an infinite loop in a thread and some exit condition that causes the thread to exit gracefully. Listing 11.6 shows a small example illustrating using a while loop in the run method and checking an exit condition before allowing the thread to exit:

Listing 11.6 Source Code for InfiniteLoopThread.java
 public class InfiniteLoopThread extends Thread {   // Default Constructor   public InfiniteLoopThread()   {     super();   }   // Override the parents run method   public void run()   {     // variable for exit condition - default to false     boolean timeToExit = false;     // Keep the thread going until time to exit is true     while( !timeToExit )     {       // Do some work here that might cause the exit       // condition to change     }     // Thread is exiting. Clean up any open resources   } } 

Notice that after the thread exits the while loop, it will exit the run method and will be considered a dead thread.

The isAlive method is a way for you to determine the state of the thread. If the isAlive method returns true, then the thread's start method has been called and the thread is not yet dead. You can't determine if the thread is currently running with this method, but you can determine whether it has been killed yet. If the isAlive method returns false, then you know that the thread is either a new thread and the start method has not been called or the thread has been terminated .

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net