Files
jvm_book/src/org/fenixsoft/jvm/chapter13/AtomicTest.java
ZhouZhiming 767555f87f init
init
2019-12-10 17:25:17 +08:00

41 lines
946 B
Java

package org.fenixsoft.jvm.chapter13;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Atomic变量自增运算测试
*
* @author zzm
*/
public class AtomicTest {
public static AtomicInteger race = new AtomicInteger(0);
public static void increase() {
race.incrementAndGet();
}
private static final int THREADS_COUNT = 20;
public static void main(String[] args) throws Exception {
Thread[] threads = new Thread[THREADS_COUNT];
for (int i = 0; i < THREADS_COUNT; i++) {
threads[i] = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10000; i++) {
increase();
}
}
});
threads[i].start();
}
while (Thread.activeCount() > 1)
Thread.yield();
System.out.println(race);
}
}