Skip to content

Commit 656f27d

Browse files
authored
Update CyclicBarrierDemo.java
1 parent 9c21711 commit 656f27d

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

CyclicBarrierDemo.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;
@@ -12,15 +10,13 @@
1210
* [1] Releases when a number of threads are waiting
1311
*/
1412
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);
1517

1618
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;
2420

2521
Counter(String name) {
2622
this.name = name;
@@ -53,9 +49,9 @@ public void run() {
5349
}
5450
}
5551

56-
public static void main(String args[]) throws InterruptedException {
52+
public static void main(String[] args) throws InterruptedException {
5753
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++) {
5955
list.add(new Counter("Adder-" + i));
6056
list.add(new Counter("Multiplier-" + i));
6157
}
@@ -65,6 +61,6 @@ public static void main(String args[]) throws InterruptedException {
6561
for (Counter c : list) {
6662
c.join();
6763
}
68-
System.out.println("Final value and is " + Counter.sum);
64+
System.out.println("Final value and is " + sum);
6965
}
7066
}

0 commit comments

Comments
 (0)