栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

volatile修饰的int与AtomicInteger原子类型的使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

volatile修饰的int与AtomicInteger原子类型的使用

volatile 修饰的int与AtomicInteger原子类型的使用区别
public class Demo01 {

    public static volatile int num;
    
    //public static AtomicInteger num2 = new AtomicInteger();

    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 5; i++) {
            threadPool.execute(() -> {
                for (int i1 = 0; i1 < 1000; i1++) {
                    add();
                }
            });
        }
        System.out.println(num);
    }

    public static void add() {
        num++;
    }


}

此段代码num的执行结果偶尔会小于4000。是因为volatile只保证可见性即每个线程去获取num时总是直接去主内存中不会去工作内存中,可以有效避免了脏读。但是自增操作是非原子性的。
自增操作的过程分为三步:

  1. 从内存中获取
  2. 对值进行修改
  3. 写回内存

例如:num=10 当Thread1从主内存中获取到值后被阻塞了。Thread2获取值后进行自增操作,并写回主内存。此时num=11。因为自增操作不是原子性的,故Thread1获取时间片后继续执行并返回,此时返回的值会覆盖之前的。所以造成现在的结果。

如果想使用原子性的自增操作使用AtomicInteger 即可,AtomicInteger是利用CAS保证原子操作的。

本文参考:https://www.cnblogs.com/dolphin0520/p/3920373.html

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/346536.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号