今天学习AtomicMarkableReference类,该类是JUC原子包中的类,通过单元测试代码把所有public api方法跑了一遍,大致了解了底层实现
package test.java.util.concurrent.atomic;
import java.util.concurrent.atomic.AtomicMarkableReference;
import org.junit.Test;
/**
* AtomicMarkableReference的测试类
*AtomicMarkableReference 用于标记一个对象的引用,通过静态内部类Pair的方式记录
*
* @author zqw
* @date 2020-06-20 20:58:44
*/
public class AtomicMarkableReferenceTest {
/**
* 构造函数,参数一为要标记的对象引用
* 参数二为标记布尔值
* toString 返回ObjectclassName@hashCode()
* void
* @throws
* @date 2020/6/20 21:40
*/
@Test
public void testConstruct0()throws Exception{
AtomicMarkableReferenceTest test=new AtomicMarkableReferenceTest();
AtomicMarkableReference testObj=new AtomicMarkableReference(test,true);
System.out.println(testObj.toString());
}
/**
* 获取引用对象
* @throws
* @date 2020/6/20 21:40
*/
@Test
public void testGetReference(){
AtomicMarkableReferenceTest test=new AtomicMarkableReferenceTest();
AtomicMarkableReference testObj=new AtomicMarkableReference(test,true);
System.out.println(testObj.getReference());
}
/**
* 获取引用对象标记值
* @throws
* @date 2020/6/20 21:40
*/
@Test
public void testIsMarked(){
AtomicMarkableReferenceTest test=new AtomicMarkableReferenceTest();
AtomicMarkableReference testObj=new AtomicMarkableReference(test,true);
System.out.println(testObj.isMarked());
}
/**
* 获取引用对象标记值
* @throws
* @date 2020/6/20 21:40
*/
@Test
public void testGet(){
AtomicMarkableReferenceTest test=new AtomicMarkableReferenceTest();
AtomicMarkableReference testObj=new AtomicMarkableReference(test,false);
boolean[] booleans=new boolean[]{true,false};
System.out.println(testObj.get(booleans));
System.out.println();
}
/**
* CompareAndSet和WeakCompareAndSet相同
* 将引用对象根据期望的值判断,true则设置成新值和新标记值
* false则不设置
* @throws
* @date 2020/6/20 21:40
*/
@Test
public void testWeakCompareAndSet(){
AtomicMarkableReferenceTest test=new AtomicMarkableReferenceTest();
AtomicMarkableReferenceTest test1=new AtomicMarkableReferenceTest();
AtomicMarkableReference testObj=new AtomicMarkableReference(test,true);
System.out.println(testObj.weakCompareAndSet(test,test1,true,false));
System.out.println(testObj.getReference());
System.out.println(testObj.isMarked());
}
/**
* CompareAndSet和WeakCompareAndSet相同
* 将引用对象根据期望的值判断,true则设置成新值和新标记值
* false则不设置
* @throws
* @date 2020/6/20 21:40
*/
@Test
public void testCompareAndSet(){
AtomicMarkableReferenceTest test=new AtomicMarkableReferenceTest();
AtomicMarkableReferenceTest test1=new AtomicMarkableReferenceTest();
AtomicMarkableReference testObj=new AtomicMarkableReference(test,true);
System.out.println(testObj.compareAndSet(test,test1,true,false));
System.out.println(testObj.getReference());
System.out.println(testObj.isMarked());
}
/**
* 将引用对象设置成新值和新标记值
* @throws
* @date 2020/6/20 21:40
*/
@Test
public void testSet(){
AtomicMarkableReferenceTest test=new AtomicMarkableReferenceTest();
AtomicMarkableReferenceTest test1=new AtomicMarkableReferenceTest();
AtomicMarkableReference testObj=new AtomicMarkableReference(test,true);
testObj.set(test1,false);
System.out.println(testObj.isMarked());
}
/**
*改变引用对象标记值
* @throws
* @date 2020/6/20 21:40
*/
@Test
public void testAttemptMark(){
AtomicMarkableReferenceTest test=new AtomicMarkableReferenceTest();
AtomicMarkableReference testObj=new AtomicMarkableReference(test,true);
testObj.attemptMark(test,false);
System.out.println(testObj.isMarked());
}
}