AtomicInteger使用说明

1.概述

java.util.concurrent.atomic.AtomicInteger

高并发的情况下,i++无法保证原子性,往往会出现问题,所以引入AtomicInteger类。

2.代码实例

2.1不使用AcomicInteger

AtomicIntegerDemo 类

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;

/**
 * <p> @Title AtomicIntegerDemo
 * <p> @Description AtomicInteger测试
 *
 * @author ACGkaka
 * @date 2020/4/25 10:26
 */
public class AtomicIntegerDemo {
    static CountDownLatch countDownLatch = new CountDownLatch(10);
    static int i = 0;
    public static class AddThread implements Runnable {
        @Override
        public void run() {
            try {
                for (int j = 0; j < 10_000; j++) {
                    i++;
                }
            } finally {
                countDownLatch.countDown();;
            }
        }
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        Thread[] threads = new Thread[10];
        for (int j = 0; j < 10; j++) {
            threads[j] = new Thread(new AddThread());
        }
        for (int k = 0; k < 10; k++) {
            threads[k].start();
        }
        countDownLatch.await();
        System.out.println("result: " + i);
    }
}

输出结果:

result: 61233

2.2使用AtomicInteger

AtomicIntegerDemo 类

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p> @Title AtomicIntegerDemo
 * <p> @Description AtomicInteger测试
 *
 * @author ACGkaka
 * @date 2020/4/25 10:26
 */
public class AtomicIntegerDemo {
    static CountDownLatch countDownLatch = new CountDownLatch(10);
    static AtomicInteger i = new AtomicInteger();
    public static class AddThread implements Runnable {
        @Override
        public void run() {
            try {
                for (int j = 0; j < 10_000; j++) {
                    i.getAndIncrement();
                }
            } finally {
                countDownLatch.countDown();;
            }
        }
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        Thread[] threads = new Thread[10];
        for (int j = 0; j < 10; j++) {
            threads[j] = new Thread(new AddThread());
        }
        for (int k = 0; k < 10; k++) {
            threads[k].start();
        }
        countDownLatch.await();
        System.out.println("result: " + i);
    }
}

输出结果:

result: 100000

3.结论

​ 不使用AtomicInteger:result: 61233

​ 使用AtomicInteger:result: 100000

​ 通过对比可以看到,不使用AtomicInteger的时候,由于i++是非原子操作,i++ 相当于 i=i+1,实际上是进行了三个操作,读取i的值,加1,写入i的值,所以当多线程并发执行的时候就会出现问题,通过使用AtomicInteger().getAndIncrement()方法可以将i++的三个操作合并为一个原子操作,不会被线程时间片的切换而中断,从而保证了最终结果的正确性。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不愿放下技术的小赵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值