Skip to content

Commit 2914172

Browse files
committed
AtomicIntegerDemo
1 parent 104bc5d commit 2914172

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package chapter4;
2+
3+
import java.util.concurrent.atomic.AtomicInteger;
4+
5+
6+
public class AtomicIntegerDemo {
7+
static AtomicInteger i = new AtomicInteger();
8+
9+
public static class AddThread implements Runnable {
10+
11+
@Override
12+
public void run() {
13+
for (int j = 0; j < 10002; j++) {
14+
i.incrementAndGet();
15+
}
16+
}
17+
}
18+
19+
public static void main(String args[]) throws InterruptedException {
20+
21+
Thread[] threads = new Thread[10];
22+
23+
for (int j = 0; j < 10; j++) {
24+
threads[j] = new Thread(new AddThread());
25+
}
26+
27+
for (int j = 0; j < 10; j++) {
28+
threads[j].start();
29+
}
30+
for (int j = 0; j < 10; j++) {
31+
threads[j].join();
32+
}
33+
34+
System.out.println(i);
35+
}
36+
}

0 commit comments

Comments
 (0)