Threads in Java

Home Up Search Java 2 API C++ Resources

A thread is a single sequence of executed code.   Java supports multi-threaded applications and applets that can run many threads simultaneously, within the abilities of the underlying operating system to run things anything simultaneously.

Threads are not objects.  Threads can run in and out of objects and many threads can be be running a given method or accessing a given property at the same time.   This fact is very important and is the root of most of the issues involved with inter-thread interactions.  For instance, the programmer must be very concerned with what happens if more than one thread is trying to change or access a piece of data at the same time ("synchronization").   Or what happens if when threads are waiting for eachother ("racing" and "locking").   A full discussion of these issues are beyond the scope of this web page, though there is a brief discussion below.

There are two ways to create ("spawn" or "fork") a thread is in Java:  subclassing the Thread class or using the Runnable interface:

The Thread Class

The Thread class is not to be confused with the thread itself.   The Thread is an encapsulation of the thread construction and gives a way of communicating with the thread as an entity.  The Thread class is used to spawn the thread and can be used to start or pause the thread.

The Thread class has three main methods:

  1. start() : This method is called by the user of this class to spawn the new thread.   start() executes the run() method as the new thread.
  2. run() : This is the code that is to be run in the separate thread.   This method can do whatever any normal method can do, such as call methods of other objects, instantiate objects, etc.
  3. sleep([milliseconds]) :  Causes the thread to pause for the specified number of miliseconds.   It is much, much more preferable to put a thread to sleep rather than have it execute an endless loop waiting for something to happen as sleeping does not require cpu resources.   Slow applications can actually be speeded up by strategically placed sleep() commands.  sleep() is a static method and the call, "Thread.sleep(n)", will cause the current thread to sleep for n milliseconds.

To spawn a new thread using the Thread class:

  1. Subclass the Thread class.
  2. Override the run() method.
  3. Instantiate the Thread object.
  4. Call its start() method at the point where you want to spawn the new thread.

The Runnable Interface

The Runnable interface specifies only one method, which takes no parameters:

public void run();

This method effectively replaces the run() method of the Thread class and frees the programmer from the constraints of having to subclass Thread.

A Thread object must still be made, as its start() method is still needed.

To spawn a thread using the Runnable interface:

  1. Have the desired class implement the Runnable interface.
  2. Implement the run() method.
  3. Where needed, instantiate a Thread object using the Thread(Runnable target) or one of the other constructors that takes a Runnable target.
  4. Call the Thread object's start() method at the point where you want to spawn the new thread.

Thread Synchronization Issues

The Volatile Keyword

This keyword is used in front of a property declaration to indicate to the compiler to never assume that it knows the value of this property and to always reread its value.   The volatile keyword is needed when one thread changes the value of a property and another thread reads it.

The Synchronized Keyword

This keyword is used in front of a method declaration to prevent multiple threads from simultaneously executing the method.   If one thread is executing the method, then all other threads are "locked" out and wait for the first method to finish before they get their turn.   A full discussion of thread scheduling is beyond the scope of this web page.   The synchronized keyword is needed when the method requires that only one thread be active at a time, for instance, when all the threads use the method to update the value of a single property and that value is dependent on the values from all the threads, e.g. a sum.

The Object.wait() method

This method, inherited by all Objects, causes the current thread to immediately pause and wait indefinitely for a notify command.    This method is used to synchronize multiple threads.

The Object.notify() method

This object is called to restart a waiting thread.