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