Notes on Java Threads

 

A thread is a separate flow of execution.  Most programs in other languages have a single thread of execution.  In Java, it is easy to create multiple threads.  The CPU is timeshared between the multiple threads.  If the computer has multiple CPUs, multiple threads can run simultaneously.

There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. An instance of the subclass can then be allocated and started. For example, a thread that computes primes larger than a stated value could be written as follows:

     class PrimeThread extends Thread {
         long minPrime;
         PrimeThread(long minPrime) {
             this.minPrime = minPrime;
         }
 
         public void run() {
             // compute primes larger than minPrime
              . . .
         }
     }

The following code would then create a thread and start it running:

     PrimeThread p = new PrimeThread(143);
     p.start();

The other way to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started.  This method works well when the class already extends another class, such as Applet.  The same example in this other style looks like the following:

     class PrimeRun implements Runnable {
         long minPrime;
         PrimeRun(long minPrime) {
             this.minPrime = minPrime;
         }
 
         public void run() {
             // compute primes larger than minPrime
              . . .
         }
     }
 

The following code would then create a thread and start it running:

     PrimeRun p = new PrimeRun(143);
     new Thread(p).start();
 

Methods that are declared with the synchronized modifier will never have more than one thread executing in them at a time.  Only one thread can execute in any synchronized method of an object at one time.

 

public class noConflict {

//  define a queue

public synchronized Widget putOnQueue() {

     // remove x from the queue

}

public synchronized void putOnQueue(Widget x) {

     // put x on the queue

}

}

 

A thread can suspend itself and wait until another thread starts it again.

 

public final void wait() throws InterruptedException

Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.

 

public final void notify()

Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened.

 

public final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

 

static public void sleep(long milliseconds)

            Suspends the execution of the thread for the specified number of milliseconds.

 

public class noConflict {

//  define a queue

int qSize = 0;

 

public synchronized Widget putOnQueue() {

     if (qSize == 0) {

          try {

              wait();

          } catch (InterruptedException e) {

              // handle error

          }

     }

     qSize--;

     return Widget from queue;

}

public synchronized void putOnQueue(Widget x) {

     // put x on the queue

     qSize++;

     notify();

}

}