JUC--Atomic

对数字进行多线程递增操作

    static AtomicLong atomicLong = new AtomicLong(0);
	static volatile int num = 0;
	static void incr() {
		num++;
	}
	public static void main(String[] args) throws Exception {
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 1000; j++) {
				new Thread(() -> {
					atomicLong.getAndIncrement();
					incr();
				}).start();
			}
		}
		while (Thread.activeCount() > 2) {
            Thread.yield();
        }
		System.out.println("Atomic:"+atomicLong);
		System.out.println("int:"+num);
	}

结果-------
可以看到同样是进行递增操作,普通的int在进行 num++ 操作是非原子性的,atomic包下边的类进行递增可以保证原子性
在这里插入图片描述
AtomicLong 的CAS操作

   static AtomicLong atomicLong = new AtomicLong(0);
	public static void main(String[] args) throws Exception {
		new Thread(()-> {
			boolean compareAndSet = atomicLong.compareAndSet(0, 1);
			System.out.println(Thread.currentThread().getName()+"   "+compareAndSet);
		},"A").start();
		new Thread(()-> {
			boolean compareAndSet = atomicLong.compareAndSet(0, 1);
			System.out.println(Thread.currentThread().getName()+"   "+compareAndSet);
		},"B").start();
		TimeUnit.SECONDS.sleep(1);
		System.out.println("Atomic:"+atomicLong);
	}

结果
在这里插入图片描述

CAS的ABA问题

static AtomicLong atomicLong = new AtomicLong(0);
	public static void main(String[] args) throws Exception {
		new Thread(()-> {
			boolean compareAndSet = atomicLong.compareAndSet(0, 1);
			System.out.println(Thread.currentThread().getName()+"   "+compareAndSet);
		},"A").start();
		new Thread(()-> {
			boolean compareAndSet = atomicLong.compareAndSet(1, 0);
			System.out.println(Thread.currentThread().getName()+"   "+compareAndSet);
		},"C").start();
		TimeUnit.SECONDS.sleep(1);
		new Thread(()-> {
			boolean compareAndSet = atomicLong.compareAndSet(0, 1);
			System.out.println(Thread.currentThread().getName()+"   "+compareAndSet);
		},"B").start();
		TimeUnit.SECONDS.sleep(1);
		System.out.println("Atomic:"+atomicLong);
	}

结果
在这里插入图片描述
为了防止ABA问题atomic包下的 AtomicStampedReference 利用版本进行控制

    static AtomicStampedReference<Long> atomicStampedReference = new AtomicStampedReference<Long>(0L, 0);
	public static void main(String[] args) throws Exception {
		new Thread(()-> {
			boolean compareAndSet = atomicStampedReference.compareAndSet(0L, 2L, 0, 1);
			System.out.println(Thread.currentThread().getName()+"   "+compareAndSet);
		},"A").start();
		new Thread(()-> {
			boolean compareAndSet = atomicStampedReference.compareAndSet(2L, 0L, 0, 1);
			System.out.println(Thread.currentThread().getName()+"   "+compareAndSet);
		},"C").start();
		TimeUnit.SECONDS.sleep(1);
		new Thread(()-> {
			boolean compareAndSet = atomicStampedReference.compareAndSet(0L, 1L, 0, 1);
			System.out.println(Thread.currentThread().getName()+"   "+compareAndSet);
		},"B").start();
		TimeUnit.SECONDS.sleep(1);
		System.out.println("Atomic:"+atomicStampedReference.getReference());
	}

结果-----
AtomicStampedReference构造方法初始化存在两个参数,第一个是泛型对应的对象,第二个是版本号
在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值