threadExamples
Class Producer

java.lang.Object
  extended by java.lang.Thread
      extended by threadExamples.Producer
All Implemented Interfaces:
java.lang.Runnable

public class Producer
extends java.lang.Thread

Once started, a Producer will unilaterally start producing numbers (using the increment with which it is started). As each number is produced, the run() method attempts to put it in the message box. Hence, this Producer may be as much as two numbers ahead of the consumer (the "consumer" is the Thread removing numbers from the message box), because there may be one number in the message box waiting to be consumed, and a second number waiting to be put into the message box.

A Producer will use either a SingleMessageBox or a BatonMessageBox, but not both, depending on which constructor was used.

Version:
February 27, 2008
Author:
David Matuszek

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
Producer(BatonMessageBox<java.lang.Integer> batonBox, int increment)
          Constructs a Producer that communicates by means of a BatonMessageBox, and produces values that count up by the given increment.
Producer(SingleMessageBox<java.lang.Integer> simpleBox, int increment)
          Constructs a Producer that communicates by means of a SingleMessageBox, and produces values that count up by the given increment.
 
Method Summary
 void run()
          Continuously counts up (with a random delay) and puts the next number into a message box, if the message box is available (empty).
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Producer

public Producer(SingleMessageBox<java.lang.Integer> simpleBox,
                int increment)
Constructs a Producer that communicates by means of a SingleMessageBox, and produces values that count up by the given increment.

Parameters:
simpleBox - The box to use for returning results.
increment - The amount by which to count.

Producer

public Producer(BatonMessageBox<java.lang.Integer> batonBox,
                int increment)
Constructs a Producer that communicates by means of a BatonMessageBox, and produces values that count up by the given increment.

Parameters:
batonBox - The box to use for returning results.
increment - The amount by which to count.
Method Detail

run

public void run()
Continuously counts up (with a random delay) and puts the next number into a message box, if the message box is available (empty). Pauses when it has to wait for the message box to become available.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread
See Also:
Thread.run()