AtomicArray

AtomicInteger ai = new AtomicInteger(1);
//1.获取值
System.out.println("ai.get = "+ai.get());

//2.增加指定值并获取
System.out.println("ai.addAndGet(2) = "+ai.addAndGet(2));
System.out.println("ai.get = "+ai.get());

//3.比较并设置  1.预期值 2.新值   预期值与当前比较 如果相同就设置为新值,返回值,是否设置成功
System.out.println("ai.compareAndSet(ai.get,10) = "+ai.compareAndSet(ai.get(),10));
System.out.println("ai.get"+ai.get());


//获取并递增
System.out.println("ai.getAndIncrement"+ai.getAndIncrement());
System.out.println("ai.get"+ai.get());
//递增并获取
System.out.println("ai.incrementAndGet"+ai.incrementAndGet());
System.out.println("ai.get"+ai.get());


 //懒设置(不会保证可见性)  volatile保证了可见性
 ai.lazySet(8);
//ai.set(8);  感觉跟set没什么区别  会保证可见性
System.out.println("ai.get"+ai.get());
posted @ 2023-12-05 21:57  会秃头的小白  阅读(4)  评论(0编辑  收藏  举报