多线程AtomicInteger原子类

1.使用原子类,不需要加锁也可以实现线程安全,避免synchronized高开销,提高效率[多线程插入属于归并结果用到]
2.原理:CAS+VOLATILE+NATIVE本地方法来保证原子性

package com.wlfy.www.exclusivelock;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author wl
 * @description AtomicIntegerTest
 * @date 2022/4/6 19:30
 */
public class AtomicIntegerTest {
    public static void main(String[] args) {
        testABA();

        testAtomicInteger();
    }

    private static void testAtomicInteger() {
        int temValue = 0;
        AtomicInteger atomicInteger = new AtomicInteger(9);
        //获取当前值并且设置新的值
        temValue = atomicInteger.getAndSet(3);
        System.out.println(temValue + ":" + atomicInteger);
        //获取当前值并自增
        temValue = atomicInteger.getAndIncrement();
        System.out.println(temValue + ":" + atomicInteger);
        //获取当前值并加上预期的值
        temValue = atomicInteger.getAndAdd(5);
        System.out.println(temValue + ":" + atomicInteger);

    }

    public static void testABA() {
        final AtomicInteger atomicInteger = new AtomicInteger(1);
        new Thread(() -> {
            final int currentValue = atomicInteger.get();
            System.out.println(Thread.currentThread().getName() + "--- currentValue=" + currentValue);

            //模拟处理其他业务花费的时间
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            boolean casRest = atomicInteger.compareAndSet(1, 2);
            System.out.println(Thread.currentThread().getName() + "--- currentValue=" + currentValue
                    + ", finalValue=" + atomicInteger.get() + ", compareAndSet casRest=" + casRest);
        }).start();

        //让上面线程先跑起来
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(() -> {
            int currentValue = atomicInteger.get();
            boolean casRest = atomicInteger.compareAndSet(1, 2);

            System.out.println(Thread.currentThread().getName() + "--- currentValue=" + currentValue
                    + ", finalValue=" + atomicInteger.get() + ", compareAndSet casRest=" + casRest);
            currentValue = atomicInteger.get();
            casRest = atomicInteger.compareAndSet(2, 1);
            System.out.println(Thread.currentThread().getName() + "--- currentValue=" + currentValue
                    + ", finalValue=" + atomicInteger.get() + ", compareAndSet casRest=" + casRest);
        }).start();
    }

}

结果:
Thread-0--- currentValue=1
Thread-0--- currentValue=1, finalValue=2, compareAndSet casRest=true
9:3
3:4
4:9
Thread-1--- currentValue=2, finalValue=2, compareAndSet casRest=false
Thread-1--- currentValue=2, finalValue=1, compareAndSet casRest=true

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值