|
Threads can be in one of four states:
Each of these states is explained in the sections that follow. New ThreadsWhen you create a thread with the new operatorfor example, new THRead(r)the thread is not yet running. This means that it is in the new state. When a thread is in the new state, the program has not started executing code inside of it. A certain amount of bookkeeping needs to be done before a thread can run. Runnable ThreadsOnce you invoke the start method, the thread is runnable. A runnable thread may or may not actually be running. It is up to the operating system to give the thread time to run. (The Java specification does not call this a separate state, though. A running thread is still in the runnable state.) NOTE
Once a thread is running, it doesn't necessarily keep running. In fact, it is desirable if running threads occasionally pause so that other threads have a chance to run. The details of thread scheduling depend on the services that the operating system provides. Preemptive scheduling systems give each runnable thread a slice of time to perform its task. When that slice of time is exhausted, the operating system preempts the thread and gives another thread an opportunity to work (see Figure 1-4 on page 27). When selecting the next thread, the operating system takes into account the thread prioritiessee page 19 for more information on priorities. Figure 1-4. Simultaneous access by two threadsAll modern desktop and server operating systems use preemptive scheduling. However, small devices such as cell phones may use cooperative scheduling. In such a device, a thread loses control only when it calls a method such as sleep or yield. On a machine with multiple processors, each processor can run a thread, and you can have multiple threads run in parallel. Of course, if there are more threads than processors, the scheduler still has to do time-slicing. Always keep in mind that a runnable thread may or may not be running at any given time. (This is why the state is called "runnable" and not "running.") Blocked ThreadsA thread enters the blocked state when one of the following actions occurs:
Figure 1-3 shows the states that a thread can have and the possible transitions from one state to another. When a thread is blocked (or, of course, when it dies), another thread can be scheduled to run. When a blocked thread is reactivated (for example, because it has slept the required number of milliseconds or because the I/O it waited for is complete), the scheduler checks to see if it has a higher priority than the currently running thread. If so, it preempts the current thread and picks a new thread to run. Figure 1-3. Thread statesA thread moves out of the blocked state and back into the runnable state by one of the following pathways.
A blocked thread can only reenter the runnable state through the same route that blocked it in the first place. In particular, you cannot simply call the resume method to unblock a blocking thread. TIP
Dead ThreadsA thread is dead for one of two reasons:
In particular, you can kill a thread by invoking its stop method. That method throws a ThreadDeath error object that kills the thread. However, the stop method is deprecated, and you should not call it in your own code. To find out whether a thread is currently alive (that is, either runnable or blocked), use the isAlive method. This method returns true if the thread is runnable or blocked, false if the thread is still new and not yet runnable or if the thread is dead. NOTE
java.lang.Thread 1.0
|
|