Section 14.4. Thread States and Life Cycle


[Page 668 (continued)]

14.4. Thread States and Life Cycle

Every thread has a life cycle that consists of several different states, as summarized in Figure 14.6 and Table 14.1. Thread states are represented by labeled ovals, and transitions between states are represented by labeled arrows. Much of a thread's life cycle is under the control of the operating system and the Java Virtual machine. Transitions represented by method namessuch as start(), stop(), wait(), sleep(), notify()can be controlled by the program. Of these methods, the stop() method has been deprecated in JDK 1.2 because it is inherently unsafe to stop a thread in the middle of its execution. Other transitionssuch as dispatch, I/O request, I/O done, time expired, done sleepingare under the control of the CPU scheduler. When first created, a thread is in the ready state, which means that it is ready to run. In the ready state, a thread is waiting in the ready queue, perhaps with other threads, for its turn on the CPU. A queue is like a waiting line. When the CPU becomes available, the first thread in the ready queue will be dispatchedthat is, it will be given the CPU. It will then be in the running state.

Figure 14.6. A thread's life cycle.


Table 14.1. Summary of thread states
(This item is displayed on page 669 in the print version)

State

Description

Ready

The thread is ready to run and waiting for the CPU.

Running

The thread is executing on the CPU.

Waiting

The thread is waiting for some event to happen.

Sleeping

The thread has been told to sleep for a time.

Blocked

The thread is waiting for I/O to finish.

Dead

The thread is terminated.


Ready, running, and sleeping


Controlling a thread


The ready queue



[Page 669]

Transitions between the ready and running states happen under the control of the CPU scheduler, a fundamental part of the Java runtime system. The job of scheduling many threads in a fair and efficient manner is a little like sharing a single bicycle among several children. Children who are ready to ride the bike wait in line for their turn. The grown-up (scheduler) lets the first child (thread) ride for a period of time before the bike is taken away and given to the next child in line. In round-robin scheduling, each child (thread) gets an equal amount of time on the bike (CPU).

CPU scheduler


When a thread calls the sleep() method, it voluntarily gives up the CPU, and when the sleep period is over, it goes back into the ready queue. This would be like one of the children deciding to rest for a moment during his or her turn. When the rest is over, the child gets back in line.

When a thread calls the wait() method, it voluntarily gives up the CPU, but this time it won't be ready to run again until it is notified by some other thread.

Threads can give up the CPU


This would be like one child giving his or her turn to another child. When the turn is up, the second child notifies the first child, who will then get back in line.

The system also manages transitions between the blocked and ready states. A thread is put into a blocked state when it does some kind of I/O operation. I/O devices, such as disk drives, modems, and keyboards, are very slow compared to the CPU. Therefore, I/O operations are handled by separate processors known as controllers. For example, when a thread wants to read data from a disk drive, the system will give this task to the disk controller, telling it where to place the data. The thread is blocked because it can't do anything until the data are read, so another thread is allowed to run. When the disk controller completes the I/O operation, the blocked thread is unblocked and placed back in the ready queue.

Threads block on I/O operations


In terms of the bicycle analogy, blocking a thread would be like giving the bicycle to another child when the rider has to stop to tie a shoe. Instead of letting the bicycle just sit there, we let another child ride it. When the shoe is tied, the first child is ready to ride again and goes back into the ready line. Letting other threads run while one thread is waiting for an I/O operation to complete improves the overall utilization of the CPU.

Self-Study Exercise

Exercise 14.5

Round-robin scheduling isn't always the best idea. Sometimes priority scheduling leads to a better system. Can you think of ways that priority schedulinghigher-priority threads go to the head of the linecan be used to improve the responsiveness of an interactive program?




Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

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