Skip to content

Commit 6bbb7d4

Browse files
authored
Update CountDownLatchDemo.java
1 parent 0f86cb4 commit 6bbb7d4

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

CountDownLatchDemo.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
package main.concurrency;
2-
31
import java.util.ArrayList;
42
import java.util.List;
53
import java.util.concurrent.BrokenBarrierException;
@@ -13,15 +11,13 @@
1311
* [1] Releases when a count value reaches zero
1412
*/
1513
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);
1618

1719
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;
2521

2622
Counter(String name) {
2723
this.name = name;
@@ -52,9 +48,9 @@ public void run() {
5248
}
5349
}
5450

55-
public static void main(String args[]) throws InterruptedException {
51+
public static void main(String[] args) throws InterruptedException {
5652
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++) {
5854
list.add(new Counter("Adder-" + i));
5955
list.add(new Counter("Multiplier-" + i));
6056
}
@@ -64,6 +60,6 @@ public static void main(String args[]) throws InterruptedException {
6460
for (Counter c : list) {
6561
c.join();
6662
}
67-
System.out.println("Final value and is " + Counter.sum);
63+
System.out.println("Final value and is " + sum);
6864
}
6965
}

0 commit comments

Comments
 (0)