28. Containers, Layout Managers, and Borders

 
[Page 803 ( continued )]

24.16. Thread States

Tasks are executed in threads. Threads can be in one of five states: New, Ready, Running, Blocked, or Finished (see Figure 24.26).

Figure 24.26. A thread can be in one of five states: New, Ready, Running, Blocked, or Finished.
(This item is displayed on page 804 in the print version)

When a thread is newly created, it enters the New state . After a thread is started by calling its start() method, it enters the Ready state . A ready thread is runnable but may not be running yet. The operating system has to allocate CPU time to it.

When a ready thread begins executing, it enters the Running state . A running thread may enter the Ready state if its given CPU time expires or its yield() method is called.

A thread can enter the Blocked state (i.e., become inactive) for several reasons. It may have invoked the join() , sleep() , wait() , or lock() method, or some other thread may have invoked these methods . It may be waiting for an I/O operation to finish. A blocked thread may be reactivated when the action inactivating it is reversed . For example, if a thread has been put to sleep and the sleep time has expired , the thread is reactivated and enters the Ready state.


[Page 804]

Finally, a thread is finished if it completes the execution of its run() method.

The isAlive() method is used to find out the state of a thread. It returns true if a thread is in the Ready , Blocked , or Running state; it returns false if a thread is new and has not started or if it is finished.

The interrupt() method interrupts a thread in the following way: If a thread is currently in the Ready or Running state, its interrupted flag is set; if a thread is currently blocked, it is awakened and enters the Ready state, and a java.lang.InterruptedException is thrown.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

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