threadExamples
Class Main
java.lang.Object
threadExamples.Main
public class Main
- extends java.lang.Object
This program demonstrates three ways to keep Threads
in sync. The first approach uses a Counter class to
generate numbers; the second and third approaches each
use a customized version of the Producer class to
generate numbers. In each case, two Threads are used
to produce numbers: One Thread counts from 1 to 10,
while the second Thread counts by tens from 10 to 100.
Because this is a demonstration program, the Counter
and Producer classes each introduce random delays in the
time it takes them to provide the next number. (You would
not normally do this is a "real" program.) Thus, the
order in which numbers are provided is not predictable.
In each case, the important thing to notice is that, while
the random delays in the Counter and Producer methods mean
that one sometimes gets ahead of the other, nonetheless, each
of the three methods in this class
(useChannels, useSingleMessageBoxes,
and useBatonMessageBoxes) succeeds in synchronizing
(pairing up) the results correctly. You can see this because
the output lines beginning with "-- Got" show the correct
corresponding values.
- Version:
- February 27, 2008
- Author:
- David Matuszek
|
Constructor Summary |
Main()
|
|
Method Summary |
static void |
main(java.lang.String[] args)
Demonstration program to synchronize two Threads--one counting
from 1 to 10, the other counting by tens from 10 to 100--in
three different ways. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Main
public Main()
main
public static void main(java.lang.String[] args)
- Demonstration program to synchronize two Threads--one counting
from 1 to 10, the other counting by tens from 10 to 100--in
three different ways.
- Parameters:
args - the command line arguments