看了前面的atomicInteger 这个就比较简单了
atomicBoolean 的使用
@Test public void testCreateWithOutArguments(){ AtomicBoolean bool = new AtomicBoolean(); assertFalse(bool.get()); } @Test public void testCreateWithArguments(){ AtomicBoolean bool = new AtomicBoolean(true); assertTrue(bool.get()); } /** * 先get 然后set */ @Test public void testGetAndSet(){ AtomicBoolean bool = new AtomicBoolean(true); boolean result = bool.getAndSet(false); assertTrue(result); assertFalse(bool.get()); } /** * 就是期望值是true 如果是true会改变成false */ @Test public void tertCompareAndSet(){ AtomicBoolean bool = new AtomicBoolean(true); boolean result = bool.compareAndSet(true, false); assertTrue(result); assertFalse(bool.get()); } @Test public void tertCompareAndSetFailed(){ AtomicBoolean bool = new AtomicBoolean(true); boolean result = bool.compareAndSet(false, true); assertTrue(bool.get()); assertFalse(result); }
atomicLong
/** * 和cup 通信 * 1.数据总线 * 2.控制总线->告诉cpu的命令 * 3.地址总线 ->告诉你这东西在内存的中的地址 */ @Test public void testCreate(){ AtomicLong atomicLong = new AtomicLong(100); /** * 判断jvm是不是free lock 如果不支持可能对数据总线尽心加锁 * static final boolean VM_SUPPORTS_LONG_CAS = VMSupportsCS8(); * 比integer 多了一个这个变量 * long 64 * * high 32 俩次传送不能保证原子性 * low 32 */ }