分析volatile关键字可以从这三个方面分析,什么是程序的原子性,什么是程序的可见性,什么是程序的有序性

以下语句那些是原子操作?

Java多线程技术-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);
      }
}
View Code

相关文章:

  • 2021-12-14
  • 2021-04-08
  • 2022-02-26
  • 2022-12-23
  • 2021-11-21
  • 2021-10-14
  • 2021-09-23
  • 2021-12-09
猜你喜欢
  • 2021-07-24
  • 2021-11-22
  • 2021-10-22
  • 2021-11-09
  • 2021-05-16
  • 2022-12-23
相关资源
相似解决方案