threadExamples
Class Counter

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

public class Counter
extends java.lang.Thread

This class illustrates the use of a Channel (a pair of Queues) to provide two-way communication between Threads. When a request is received via a Channel, a response (an incremented number) is returned in the same Channel.

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
Counter(Channel<?,java.lang.Integer> c, int increment)
          Creates a Counter.
 
Method Summary
static void randomDelay()
          Pauses a random amount of time.
 void run()
          When a request is received (via the request queue of a Channel), then after a random delay, a response (an incremented number) is produced and returned (via the response queue of the same Channel).
 
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

Counter

public Counter(Channel<?,java.lang.Integer> c,
               int increment)
Creates a Counter.

Parameters:
c - The Channel to be used for communication.
increment - The amount by which to count.
Method Detail

run

public void run()
When a request is received (via the request queue of a Channel), then after a random delay, a response (an incremented number) is produced and returned (via the response queue of the same Channel).

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

randomDelay

public static void randomDelay()
Pauses a random amount of time.