package thread;

public class VolatileTest {

public static volatile int race = 0;

public static void increase(){

race++;

}

private static final int THREAD_COUNT = 20;

public static void main(String[] args) {

Thread[] threads = new Thread[THREAD_COUNT];

for(int i = 0;i<THREAD_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);

}

}

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐