今天學習AtomicLong類,該類是JUC原子包中的類,通過單元測試代碼把所有public api方法跑了一遍,大致瞭解了底層實現
package test.java.util.concurrent.atomic;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.IntBinaryOperator;
import java.util.function.IntUnaryOperator;
import java.util.function.LongBinaryOperator;
import java.util.function.LongUnaryOperator;
import org.junit.Test;
/**
* AtomicLong的測試類
*
* @author zqw
* @date 2020-06-18 22:52:36
*/
public class AtomicLongTest {
/**
* 通過整數值初始化構造函數
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testConstructLong(){
AtomicLong atomicLong=new AtomicLong(3L);
System.out.println(atomicLong.get());
}
/**
* 無參構造函數,默認爲0
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testConstructEmpty(){
AtomicLong atomicLong=new AtomicLong();
System.out.println(atomicLong.get());
}
/**
* 獲取value值
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testGet(){
AtomicLong atomicLong=new AtomicLong();
System.out.println(atomicLong.get());
}
/**
* 設置value值
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testSet(){
AtomicLong atomicLong=new AtomicLong(4);
atomicLong.set(43L);
System.out.println(atomicLong.get());
}
/**
* 設置value值
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testLazySet(){
AtomicLong atomicLong=new AtomicLong(3);
atomicLong.lazySet(33);
System.out.println(atomicLong.get());
}
/**
* 獲取value值並設置成新值
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testGetAndSet(){
AtomicLong atomicLong=new AtomicLong(3);
System.out.println(atomicLong.getAndSet(223));
System.out.println(atomicLong.get());
}
/**
* 如果value爲expect參數值,則更新成新值並返回true
* 否則返回false
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testCompareAndSet(){
AtomicLong atomicLong=new AtomicLong(3);
System.out.println(atomicLong.compareAndSet(3,33));
System.out.println(atomicLong.get());
}
/**
* 如果value爲expect參數值,則更新成新值並返回true
* 否則返回false
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testWeakCompareAndSet(){
AtomicLong atomicLong=new AtomicLong(3);
System.out.println(atomicLong.weakCompareAndSet(3,23));
System.out.println(atomicLong.get());
}
/**
* 獲取value值並將值加一
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testGetAndIncrement(){
AtomicLong atomicLong=new AtomicLong(3);
System.out.println(atomicLong.getAndIncrement());
System.out.println(atomicLong.get());
}
/**
* 獲取value值並將值減一
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testGetAndDecrement(){
AtomicLong atomicLong=new AtomicLong(3);
System.out.println(atomicLong.getAndDecrement());
System.out.println(atomicLong.get());
}
/**
* 獲取value值並將值加一
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testGetAndAdd(){
AtomicLong atomicLong=new AtomicLong(3);
System.out.println(atomicLong.getAndAdd(3));
System.out.println(atomicLong.get());
}
/**
* 將值加一併獲取value值
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testIncrementAndGet(){
AtomicLong atomicLong=new AtomicLong(3);
System.out.println(atomicLong.incrementAndGet());
}
/**
* 將值減一併獲取value值
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testDecrementAndGet(){
AtomicLong AtomicLong=new AtomicLong(3);
System.out.println(AtomicLong.decrementAndGet());
}
/**
* 將值增加對應的值並獲取value值
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testAddAndGet(){
AtomicLong atomicLong=new AtomicLong(3);
System.out.println(atomicLong.addAndGet(33));
}
/**
* 獲取value值並更新成新值
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testGetAndUpdate(){
AtomicLong atomicLong=new AtomicLong(3);
LongUnaryOperator operator=new LongUnaryOperator() {
@Override
public long applyAsLong(long operand) {
return 333;
}
};
System.out.println(atomicLong.getAndUpdate(operator));
System.out.println(atomicLong.get());
}
/**
* 更新成新值並獲取value值
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testUpdateAndGet(){
AtomicLong atomicLong=new AtomicLong(3);
LongUnaryOperator operator=new LongUnaryOperator() {
@Override
public long applyAsLong(long operand) {
return 333;
}
};
System.out.println(atomicLong.updateAndGet(operator));
}
/**
* 獲取value值並更新成新值
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testGetAndAccumulate(){
AtomicLong atomicLong=new AtomicLong(3);
LongBinaryOperator operator=new LongBinaryOperator() {
@Override
public long applyAsLong(long left, long right) {
return 332;
}
};
System.out.println(atomicLong.getAndAccumulate(23,operator));
System.out.println(atomicLong.get());
}
/**
* 更新成新值並獲取value值
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testAccumulateAndGet(){
AtomicLong atomicLong=new AtomicLong(3);
LongBinaryOperator operator=new LongBinaryOperator() {
@Override
public long applyAsLong(long left, long right) {
return 332;
}
};
System.out.println(atomicLong.accumulateAndGet(23,operator));
}
/**
* Integer.toString()
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testToString(){
AtomicLong atomicLong=new AtomicLong(33);
System.out.println(atomicLong.toString());
}
/**
* 獲取value的int值
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testIntValue(){
AtomicLong atomicLong=new AtomicLong(33);
System.out.println(atomicLong.intValue());
}
/**
* 獲取value的long值
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testLongValue(){
AtomicLong atomicLong=new AtomicLong(33);
System.out.println(atomicLong.longValue());
}
/**
* 獲取value的float值
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testFloatValue(){
AtomicLong atomicLong=new AtomicLong(33);
System.out.println(atomicLong.floatValue());
}
/**
* 獲取value的double值
*
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testDoubleValue(){
AtomicLong atomicLong=new AtomicLong(33);
System.out.println(atomicLong.doubleValue());
}
}