1
- package main .concurrency ;
2
-
3
1
import java .util .ArrayList ;
4
2
import java .util .List ;
5
3
import java .util .concurrent .BrokenBarrierException ;
12
10
* [1] Releases when a number of threads are waiting
13
11
*/
14
12
public class CyclicBarrierDemo {
13
+ public static int MAX_NUM_THREADS = 10 ;
14
+ public static int sum = 0 ;
15
+ private static final Lock lock = new ReentrantLock ();
16
+ private static final CyclicBarrier barrier = new CyclicBarrier (MAX_NUM_THREADS );
15
17
16
18
private static class Counter extends Thread {
17
-
18
- public static int MAX_NUM_THREADS = 10 ;
19
- public static int sum = 0 ;
20
- private static Lock lock = new ReentrantLock ();
21
- private static CyclicBarrier barrier = new CyclicBarrier (MAX_NUM_THREADS );
22
-
23
- private String name ;
19
+ private final String name ;
24
20
25
21
Counter (String name ) {
26
22
this .name = name ;
@@ -53,9 +49,9 @@ public void run() {
53
49
}
54
50
}
55
51
56
- public static void main (String args [] ) throws InterruptedException {
52
+ public static void main (String [] args ) throws InterruptedException {
57
53
List <Counter > list = new ArrayList <>();
58
- for (int i = 0 ; i < Counter . MAX_NUM_THREADS / 2 ; i ++) {
54
+ for (int i = 0 ; i < MAX_NUM_THREADS / 2 ; i ++) {
59
55
list .add (new Counter ("Adder-" + i ));
60
56
list .add (new Counter ("Multiplier-" + i ));
61
57
}
@@ -65,6 +61,6 @@ public static void main(String args[]) throws InterruptedException {
65
61
for (Counter c : list ) {
66
62
c .join ();
67
63
}
68
- System .out .println ("Final value and is " + Counter . sum );
64
+ System .out .println ("Final value and is " + sum );
69
65
}
70
66
}
0 commit comments