AtomicReferenceArray api详解

今天学习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());
        }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章