|
|
Threads of Control |
The following diagram illustrates the various states that a Java thread can be in at any point during its life. It also illustrates which method calls cause a transition to another state. This diagram is not a complete finite state diagram, but rather an overview of the more interesting and common facets of a thread's life. The remainder of this page discusses a thread's life cycle in terms of its state.
![]()
New Thread
The following statement creates a new thread but does not start it, thereby leaving the thread in the New Thread state.When a thread is a new thread, it is merely an emptyThread myThread = new MyThreadClass();Threadobject. No system resources have been allocated for it yet. When a thread is in this state, you can only start the thread or stop it. Calling any method besidesstartorstopwhen a thread is in this state makes no sense and causes an IllegalThreadStateException.Runnable
Now consider these two lines of code:TheThread myThread = new MyThreadClass(); myThread.start();startmethod creates the system resources necessary to run the thread, schedules the thread to run, and calls the thread'srunmethod. At this point the thread is in the "Runnable" state. This state is called "Runnable" rather than "Running" because the thread might not actually be running when it is in this state. Many computers have a single processor, making it impossible to run all runnable threads at the same time. The Java runtime system must implement a scheduling scheme that shares the processor between all runnable threads. (See Thread Priority for more information about scheduling.) However, for most purposes you can think of the Runnable state as simply "Running." When a thread is running--it's runnable and is the current thread--the instructions in itsrunmethod are executing sequentially.Not Runnable
A thread becomes not runnable when one of these four events occurs:For example, the bold line in the following code snippet puts the current thread to sleep for 10 seconds (10,000 milliseconds):
- Someone invokes its
sleepmethod.- Someone invokes its
suspendmethod.- The thread uses its
waitmethod to wait on a condition variable.- The thread is blocking on I/O.
During the 10 seconds thattry { Thread.sleep(10000); } catch (InterruptedException e){ }myThreadis asleep, even if the processor becomes availablemyThreaddoes not run. After the 10 seconds are up,myThreadbecomes runnable again and, if the processor becomes available, runs. For each of the entrances into the Not Runnable state shown in the figure, there is a specific and distinct escape route that returns the thread to the Runnable state. An escape route only works for its corresponding entrance. For example, if a thread has been put to sleep, then the specified number of milliseconds must elapse before the thread becomes Runnable again. Callingresumeon a sleeping thread has no effect.The following indicates the escape route for every entrance into the Not Runnable state.
- If a thread has been put to sleep, then the specified number of milliseconds must elapse.
- If a thread has been suspended, then someone must call its
resumemethod.- If a thread is waiting on a condition variable, then whatever object owns the variable must relinquish it by calling either
notifyornotifyAll.- If a thread is blocked on I/O, then the I/O must complete.
Dead
A thread can die in two ways: either from natural causes, or by being killed (stopped). A thread dies naturally when itsrunmethod exits normally. For example, thewhileloop in this method is a finite loop--it will iterate 100 times and then exit.A thread with thispublic void run() { int i = 0; while (i < 100) { i++; System.out.println("i = " + i); } }runmethod dies naturally after the loop and therunmethod complete.You can also kill a thread at any time by calling its
stopmethod. The following code snippet creates and startsmyThread, then puts the current thread to sleep for 10 seconds. When the current thread wakes up, the bold line in the code segment killsmyThread.TheThread myThread = new MyThreadClass(); myThread.start(); try { Thread.sleep(10000); } catch (InterruptedException e){ } myThread.stop();stopmethod throws aThreadDeathobject at the thread to kill it. Thus when a thread is killed in this manner it dies asynchronously. The thread will die when it actually receives theThreadDeathexception.The
stopmethod causes a sudden termination of aThread'srunmethod. If therunmethod performs critical or sensitive calculations,stopmay leave the program in an inconsistent or awkward state. Normally, you should not callThread'sstopmethod but arrange for a gentler termination such as setting a flag to indicate to therunmethod that it should exit.
IllegalThreadStateExceptionThe runtime system throws anIllegalThreadStateExceptionwhen you call a method on a thread and that thread's state does not allow for that method call. For example,IllegalThreadStateExceptionis thrown when you invokesuspendon a thread that is not runnable.As the examples of threads in this lesson have shown, when you call a thread method that can throw an exception, you must either catch and handle the exception, or specify that the calling method throws the uncaught exception. See Handling Errors Using Exceptions
for information about exception handling in Java.
The
isAliveMethodA final word about thread state: the programming interface for theThreadclass includes a method calledisAlive. TheisAlivemethod returns true if the thread has been started and not stopped. If theisAlivemethod returns false, you know that the thread is either a new thread or dead. If theisAlivemethod returns true, you know that the thread is either runnable or not runnable. You cannot differentiate between a new thread and a dead thread; nor can you differentiate between a runnable thread and a not runnable thread.See Also
java.lang.Thread
java.lang.IllegalThreadStateException
java.lang.ThreadDeath
|
|
Threads of Control |