分析volatile关键字可以从这三个方面分析,什么是程序的原子性,什么是程序的可见性,什么是程序的有序性
以下语句那些是原子操作?
public class ThreadCounter implements Runnable { private int count = 0; @Override public void run() { ++count; // count++; } public static void main(String[] args) throws InterruptedException { ThreadCounter thread = new ThreadCounter(); for(int i = 0; i< 10000; i++){ new Thread(thread).start(); } Thread.sleep(1000);//确保线程执行完 System.out.println(thread.count); } }