All ITKnowledge Source Code Search Tips Advanced Search |

| Mastering Java Threads by Marc Adler and David Herst DDC Publishing, Inc. ISBN: 1562438425 Pub Date: 05/01/99

Search this book:

Previous | Table of Contents | Next

## Chapter 5
The Life of a Thread

Lesson Topics

  • Different Thread States

### Different Thread States

A thread can exist in many different states during its “lifetime.” Figure 5-1 displays is a list of the various states in which a thread can exist.

  • New thread
  • Runnable state
  • Not runnable state
  • Dead state

Figure 5-1 Various states of a thread

#### New Thread (New State)

A thread must somehow be created in memory. The following code accomplishes thread creation:

    Thread t = new Thread (“MyThread”);

This code creates an empty thread. There are no system resources allocated to this thread. The thread is in the new thread state. The only operations which can be performed on a new thread are:

  • to start() it
  • to stop() it

If you try to do anything else to a new thread, you will generate the following exception:

        IllegalThreadState

#### Runnable State

When you call a new thread’s start() method, the thread’s state changes from New to Runnable. After a thread is changed from New to Runnable, Java will:

  • allocate system resources to the thread
  • schedule the thread for execution
  • allocate stack space for the thread
  • call the thread’s run() method

The Java scheduler will allocate processor time among the various threads that are in the Runnable state. You can force a thread to relinquish processor time by calling its yield() method. When you call yield(), the thread will remain in the Runnable state. However, it will allow the Java scheduler to run another thread that might be waiting for its chance.

Transitions Between States

When a thread is in the Runnable state, it can transition to the Not Runnable state or the Dead state. If you call a thread’s stop() method, it will go to the Dead state. There are various ways in which a thread can go from the Runnable to the Not Runnable state. The ways are outlined on the following page.

#### Not Runnable State

Table 5-1 outlines the ways in which a thread can enter and exit a Not Runnable state.

Table 5-1: Ways in which a thread can enter or exit a Not Runnable state
Enter a Not Runnable State | Return to a Runnable State
--- | ---
A thread is blocking on I/O. | The I/O completes.
A thread uses the wait() method to wait on a condition variable. | The object which owns the variable calls notify() or notifyAll().
You call a thread’s sleep() method. | The specified number of milliseconds elapses.
You call a thread’s suspend() method. | You call the resume() method.

| When a thread is in the Not Runnable state, it receives no processor time—even if the processor is available.

#### Dead State

There are two ways in which a thread can reach the Dead state.

  1. Call the thread’s stop() method while the thread is in any of the three states (New, Runnable, Not Runnable).
  2. The Java VM executes the last line of code in the thread’s run() method. After the last line of code in the run() method has executed, the thread terminates.

The Java VM could get to the run() method’s last line of code by:

  • executing normally and finally reaching the end of the method; or
  • not catching an Exception or an Error object.

stop() and ThreadDeath

The following points pertain to the stop() method and ThreadDeath.

  • stop() kills a thread asynchronously.
  • stop() throws a ThreadDeath object (a thread will die if it receives the ThreadDeath exception).
  • The ThreadDeath object is actually a subclass of the Java Error object, not the Exceptionm object. Normally, applications do not catch Error objects.
  • Stopping a thread by throwing an object allows the stack to unwind nicely and allows synchronization objects to be destroyed cleanly.

| You should not use the destroy() method of the Thread class to terminate a thread. The destroy() method does not perform any cleanup code, and any locks which are engaged will remain locked after destroy() has performed its work.

#### IsAlive[]

The isAlive() method will return a true value if a thread is active. By “active,” it is meant that the thread is in a Runnable or Not Runnable state. If a thread is Dead or New, the isAlive() method returns a value of false.

Previous | Table of Contents | Next

Copyright © DDC Publishing

Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.