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());
        }

}

 

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