首页 > 其他 > 详细

原子类 Atomic

时间:2019-11-21 17:52:39      阅读:98      评论:0      收藏:0      [点我收藏+]

  

@Test
public void testAtomicBoolean() {
AtomicBoolean atomicBoolean = new AtomicBoolean();
boolean b = atomicBoolean.get();
System.out.println("默认值:" + b);
atomicBoolean.set(true);
System.out.println(atomicBoolean.get());
}

@Test
public void testAtomic() throws NoSuchFieldException {
System.out.println(AtomicInteger.class.getDeclaredField("value"));
AtomicInteger atomicInteger = new AtomicInteger();
int i = atomicInteger.get();
System.out.println("defaultValue:" + i);
//当前值 + 1
atomicInteger.getAndIncrement();
System.out.println("getAndIncrement:" + atomicInteger.get());
//指定增加数量 + n
atomicInteger.getAndAdd(4);
System.out.println("getAndAdd:" + atomicInteger.get());
//当前值 - 1
atomicInteger.getAndDecrement();
System.out.println("getAndDecrement:" + atomicInteger.get());
//当前值 + 1 并返回结果
System.out.println("incrementAndGet:" + atomicInteger.incrementAndGet());
//当前值 - 1 并返回结果
System.out.println("decrementAndGet:" + atomicInteger.decrementAndGet());
//当前值 + n 并返回结果
System.out.println("addAndGet:" + atomicInteger.addAndGet(5));

System.out.println("doubleValue:" + atomicInteger.doubleValue());
System.out.println("longValue:" + atomicInteger.longValue());
System.out.println("floatValue:" + atomicInteger.floatValue());
System.out.println("byteValue:" + atomicInteger.byteValue());
System.out.println("shortValue:" + atomicInteger.shortValue());
System.out.println("toString:" + atomicInteger.toString());
}

原子类 Atomic

原文:https://www.cnblogs.com/ming-blogs/p/11906936.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!