forked from benelog/java-concurrency
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CounterDemo.java
33 lines (28 loc) · 959 Bytes
/
CounterDemo.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
package s2gx.counter;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CounterDemo {
public static void main(String[] args) throws Exception {
final Counter counter = new SynchronizedCounter();
int loop = 100000;
// also try: newSingleThreadPool, newFixedThreadPool(2)
ExecutorService executor = Executors.newFixedThreadPool(100);
final CountDownLatch latch = new CountDownLatch(loop);
long start = System.currentTimeMillis();
for (int i = 0; i < loop; i++) {
executor.execute(new Runnable() {
public void run() {
counter.increment();
latch.countDown();
}
});
}
System.out.println("threads: " + Thread.activeCount());
latch.await();
long time = System.currentTimeMillis() - start;
System.out.println("time: " + time + "ms");
System.out.println("count: " + counter.increment());
executor.shutdown();
}
}