This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestBarrier.java
85 lines (70 loc) · 2.42 KB
/
TestBarrier.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.io.*;
/**
* A testing harness for the Barrier class.
*
* Proper output is that we should see is that all threads
* output an 'A' before reaching the barrier and then a 'B'
* after proceeding through the barrier. Therefore, output
* should appear as a series of 'A's followed by an equal count
* of 'B's. (There should not be an intermingling of 'A's and 'B's.
*/
public class TestBarrier
{
public static final int THREAD_COUNT = 5;
public static void main(String args[]) throws java.io.IOException {
System.out.println("Proper output is that we should see is that all threads");
System.out.println("output an 'A' before reaching the barrier and then a 'B'");
System.out.println("after proceeding through the barrier. Therefore, output");
System.out.println("should appear as a series of 'A's followed by an equal count");
System.out.println("of 'B's. (There should not be an intermingling of 'A's and 'B's.");
System.out.println("\n\nPress Enter to begin the test:");
(new BufferedReader(new InputStreamReader(System.in))).read();
// initialize the barrier to the number of threads
// waiting upon the barrier
Barrier barrier = new BarrierImpl(THREAD_COUNT);
Thread[] workers = new Thread[THREAD_COUNT];
for (int i = 0; i < workers.length; i++) {
workers[i] = new Thread(new Worker(barrier));
workers[i].start();
}
try {
for (int i = 0; i < workers.length; i++)
workers[i].join();
}
catch (InterruptedException ie) { }
System.out.println("\n\nPress Enter to begin the freeAll() test:");
(new BufferedReader(new InputStreamReader(System.in))).read();
barrier = new BarrierImpl(THREAD_COUNT + 1);
workers = new Thread[THREAD_COUNT];
for (int i = 0; i < workers.length; i++) {
workers[i] = new Thread(new Worker(barrier));
workers[i].start();
}
try {
Thread.sleep(3000);
}
catch (InterruptedException ie) { }
barrier.freeAll();
}
}
/**
* A worker using the Barrier
*/
class Worker implements Runnable
{
private Barrier partA;
public Worker(Barrier partA) {
this.partA = partA;
}
/**
* Each thread will do some work for awhile, and then
* invoke waitForOthers(). A thread may proceed when all
* other threads have arrived at the barrier.
*/
public void run() {
System.out.println("A");
SleepUtilities.nap();
partA.waitForOthers();
System.out.println("B");
}
}