Thread States


Threads can be in one of four states:

  • New

  • Runnable

  • Blocked

  • Dead

Each of these states is explained in the sections that follow.

New Threads

When 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 Threads

Once 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

The runnable state has nothing to do with the Runnable interface.


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 threads


All 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 Threads

A thread enters the blocked state when one of the following actions occurs:

  • The thread goes to sleep by calling the sleep method.

  • The thread calls an operation that is blocking on input/output, that is, an operation that will not return to its caller until input and output operations are complete.

  • The thread tries to acquire a lock that is currently held by another thread. We discuss locks on page 27.

  • The thread waits for a conditionsee page 30.

  • Someone calls the suspend method of the thread. However, this method is deprecated, and you should not call it in your code.

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 states


A thread moves out of the blocked state and back into the runnable state by one of the following pathways.

  1. If a thread has been put to sleep, the specified number of milliseconds must expire.

  2. If a thread is waiting for the completion of an input or output operation, then the operation must have finished.

  3. If a thread is waiting for a lock that was owned by another thread, then the other thread must relinquish ownership of the lock. (It is also possible to wait with a timeout. Then the thread unblocks when the timeout elapses.)

  4. If a thread waits for a condition, then another thread must signal that the condition may have changed. (If the thread waits with a timeout, then the thread is unblocked when the timeout elapses.)

  5. If a thread has been suspended, then someone must call its resume method. However, because the suspend method has been deprecated, the resume method has been deprecated as well, and you should not call it in your own code.

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

If you need to unblock an I/O operation, you should use the channel mechanism from the "new I/O" library. When another thread closes the channel, the blocked thread becomes runnable again, and the blocking operation throws a ClosedChannelException.


Dead Threads

A thread is dead for one of two reasons:

  • It dies a natural death because the run method exits normally.

  • It dies abruptly because an uncaught exception terminates the run method.

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

You cannot find out if an alive thread is runnable or blocked, or if a runnable thread is actually running. In addition, you cannot differentiate between a thread that has not yet become runnable and one that has already died.



 java.lang.Thread 1.0 

  • boolean isAlive()

    returns true if the thread has started and has not yet terminated.

  • void stop()

    stops the thread. This method is deprecated.

  • void suspend()

    suspends this thread's execution. This method is deprecated.

  • void resume()

    resumes this thread. This method is only valid after suspend() has been invoked. This method is deprecated.

  • void join()

    waits for the specified thread to die.

  • void join(long millis)

    waits for the specified thread to die or for the specified number of milliseconds to pass.



    Core JavaT 2 Volume II - Advanced Features
    Building an On Demand Computing Environment with IBM: How to Optimize Your Current Infrastructure for Today and Tomorrow (MaxFacts Guidebook series)
    ISBN: 193164411X
    EAN: 2147483647
    Year: 2003
    Pages: 156
    Authors: Jim Hoskins

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