今天學習AtomicReferenceArray類,該類是JUC原子包中的類,通過單元測試代碼把所有public api方法跑了一遍,大緻了解了底層實作
package test.java.util.concurrent.atomic;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.function.BinaryOperator;
import java.util.function.LongBinaryOperator;
import java.util.function.LongUnaryOperator;
import java.util.function.UnaryOperator;
import org.junit.Test;
/**
* AtomicReferenceArray的測試類
*
* @author zqw
* @date 2020-06-20 22:19:25
*/
public class AtomicReferenceArrayTest {
/**
* 通過size初始化AtomicReferenceArray數組類,其實就是将
* Object[] array=new Object[length];
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testConstruct0()throws Exception{
AtomicReferenceArray testObj=new AtomicReferenceArray(2);
System.out.println(testObj.toString());
}
/**
* 通過數組初始化AtomicReferenceArray數組類,其實就是将
* clone()方法是将intArr内容複制到一個新的相同大小的數組空間,
* 并将位址賦給array
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testConstruct1()throws Exception{
Integer[] intArr={11321,232,322};
AtomicReferenceArray testObj=new AtomicReferenceArray(intArr);
System.out.println(testObj.toString());
}
/**
* array.length
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testLength(){
Integer[] intArr={11321,232,322};
AtomicReferenceArray testObj=new AtomicReferenceArray(intArr);
System.out.println(testObj.length());
}
/**
* 擷取數組下标的内容,需要檢查是否越界
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testGet(){
Integer[] intArr={11321,232,322};
AtomicReferenceArray testObj=new AtomicReferenceArray(intArr);
System.out.println(testObj.get(2));
}
/**
* 設定數組下标的内容為新的值,unsafe.putIntVolatile設定值
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testSet(){
Integer[] intArr={11321,232,322};
AtomicReferenceArray testObj=new AtomicReferenceArray(intArr);
testObj.set(2,23);
System.out.println(testObj.get(2));
}
/**
* 設定數組下标的内容為新的值,unsafe.putOrderedInt
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testLazySet(){
Integer[] intArr={11321,232,322};
AtomicReferenceArray testObj=new AtomicReferenceArray(intArr);
testObj.lazySet(2,23);
System.out.println(testObj.get(2));
}
/**
* 傳回下标的值,并設定成新的值
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testGetAndSet(){
Integer[] intArr={11321,232,322};
AtomicReferenceArray testObj=new AtomicReferenceArray(intArr);
System.out.println(testObj.getAndSet(2,23));
System.out.println(testObj.get(2));
}
/**
* 如果下标的值為expect的值,則更新成新值,傳回true
* 否則不更新,傳回false
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testCompareAndSet(){
Integer[] intArr={11321,232,2};
AtomicReferenceArray testObj=new AtomicReferenceArray(intArr);
System.out.println(testObj.compareAndSet(2,2,23));
System.out.println(testObj.get(2));
}
/**
* 如果下标的值為expect的值,則更新成新值,傳回true
* 否則不更新,傳回false
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testWeakCompareAndSet(){
Integer[] intArr={11321,232,2};
AtomicReferenceArray testObj=new AtomicReferenceArray(intArr);
System.out.println(testObj.weakCompareAndSet(2,2,23));
System.out.println(testObj.get(2));
}
/**
* 傳回下标的值,并更新成新值
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testGetAndUpdate(){
Integer[] intArr={11321,232,322};
AtomicReferenceArray testObj=new AtomicReferenceArray(intArr);
UnaryOperator operator=new UnaryOperator() {
@Override
public Object apply(Object o) {
return 421421;
}
};
System.out.println(testObj.getAndUpdate(2,operator));
System.out.println(testObj.get(2));
}
/**
* 更新成新值并傳回下标的值
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testUpdateAndGet(){
Integer[] intArr={11321,232,322};
AtomicReferenceArray testObj=new AtomicReferenceArray(intArr);
UnaryOperator operator=new UnaryOperator() {
@Override
public Object apply(Object o) {
return 421421;
}
};
System.out.println(testObj.updateAndGet(2,operator));
System.out.println(testObj.get(2));
}
/**
* 傳回下标值,并更新成新值
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testGetAndAccumulate(){
Integer[] intArr={11321,232,322};
AtomicReferenceArray testObj=new AtomicReferenceArray(intArr);
BinaryOperator operator=new BinaryOperator() {
@Override
public Object apply(Object o, Object o2) {
return o2;
}
};
System.out.println(testObj.getAndAccumulate(2,23,operator));
System.out.println(testObj.get(2));
}
/**
* 更新成新值并傳回下标值
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testAccumulateAndGet(){
Integer[] intArr={11321,232,322};
AtomicReferenceArray testObj=new AtomicReferenceArray(intArr);
BinaryOperator operator=new BinaryOperator() {
@Override
public Object apply(Object o, Object o2) {
return o2;
}
};
System.out.println(testObj.accumulateAndGet(2,23,operator));
System.out.println(testObj.get(2));
}
/**
* 拼接數組的值,StringBuilder
* 空傳回[],非空傳回[2,3,1]
* @throws
* @date 2020/6/16 23:46
*/
@Test
public void testToString(){
Integer[] intArr={11321,232,322};
AtomicReferenceArray testObj=new AtomicReferenceArray(intArr);
System.out.println(testObj.toString());
}
}