LongAccumulator api 詳解

今天學習LongAccumulator類,該類是JUC原子包中的類,通過單元測試代碼把所有public api方法跑了一遍,大致瞭解了底層實現

package test.java.util.concurrent.atomic;


import java.util.concurrent.atomic.LongAccumulator;
import java.util.function.LongBinaryOperator;

import org.junit.Test;

/**
 * LongAccumulator的測試類
 *
 * @author zqw
 * @date 2020-06-20 23:51:49
 */
public class LongAccumulatorTest {
        /**
         *
         * void
         * @Param 初始化double計算器
         * @author zhqwm
         * @date 2020/6/20 23:39
         */
        @Test
        public void testConstruct0()throws Exception{
                LongBinaryOperator LongBinaryOperator=new LongBinaryOperator() {
                        @Override
                        public long applyAsLong(long left, long right) {
                                return left+right;
                        }
                };
                LongAccumulator testObj=new LongAccumulator(LongBinaryOperator,321);
                System.out.println(testObj.get());
        }
        /**
         *
         * void
         * @Param 根據operator函數規則計算
         * @author zhqwm
         * @date 2020/6/20 23:39
         */
        @Test
        public void testAccumulate(){
                LongBinaryOperator LongBinaryOperator=new LongBinaryOperator() {
                        @Override
                        public long applyAsLong(long left, long right) {
                                return left+right;
                        }
                };
                LongAccumulator testObj=new LongAccumulator(LongBinaryOperator,321);
                testObj.accumulate(3);
                System.out.println(testObj.get());
        }
        /**
         * void
         * @Param 獲取double值
         * @author zhqwm
         * @date 2020/6/20 23:39
         */
        @Test
        public void testGet(){
                LongBinaryOperator LongBinaryOperator=new LongBinaryOperator() {
                        @Override
                        public long applyAsLong(long left, long right) {
                                return left+right;
                        }
                };
                LongAccumulator testObj=new LongAccumulator(LongBinaryOperator,321);
                System.out.println(testObj.get());
        }
        /**
         *計算之後重置回原值
         * @author zhqwm
         * @date 2020/6/20 23:39
         */
        @Test
        public void testReset(){
                LongBinaryOperator LongBinaryOperator=new LongBinaryOperator() {
                        @Override
                        public long applyAsLong(long left, long right) {
                                return left+right;
                        }
                };
                LongAccumulator testObj=new LongAccumulator(LongBinaryOperator,321);
                testObj.accumulate(3);
                System.out.println(testObj.get());
                testObj.reset();
                System.out.println(testObj.get());
        }
        /**
         *先獲取當前值然後重置
         * void
         * @author zhqwm
         * @date 2020/6/20 23:39
         */
        @Test
        public void testGetThenReset(){
                LongBinaryOperator LongBinaryOperator=new LongBinaryOperator() {
                        @Override
                        public long applyAsLong(long left, long right) {
                                return left+right;
                        }
                };
                LongAccumulator testObj=new LongAccumulator(LongBinaryOperator,321);
                testObj.accumulate(3);
                System.out.println(testObj.getThenReset());
                System.out.println(testObj.get());

        }
        /**
         *
         * void
         * @Param 返回double值
         * @author zhqwm
         * @date 2020/6/20 23:39
         */
        @Test
        public void testToString(){
                LongBinaryOperator LongBinaryOperator=new LongBinaryOperator() {
                        @Override
                        public long applyAsLong(long left, long right) {
                                return left+right;
                        }
                };
                LongAccumulator testObj=new LongAccumulator(LongBinaryOperator,321);
                System.out.println(testObj.toString());
        }
        /**
         *返回long值
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/20 23:39
         */
        @Test
        public void testLongValue(){
                LongBinaryOperator LongBinaryOperator=new LongBinaryOperator() {
                        @Override
                        public long applyAsLong(long left, long right) {
                                return left+right;
                        }
                };
                LongAccumulator testObj=new LongAccumulator(LongBinaryOperator,321);
                System.out.println(testObj.longValue());
        }
        /**
         *返回int值
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/20 23:39
         */
        @Test
        public void testIntValue(){
                LongBinaryOperator LongBinaryOperator=new LongBinaryOperator() {
                        @Override
                        public long applyAsLong(long left, long right) {
                                return left+right;
                        }
                };
                LongAccumulator testObj=new LongAccumulator(LongBinaryOperator,321);
                System.out.println(testObj.intValue());
        }

        /**
         *返回float值
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/20 23:39
         */
        @Test
        public void testFloatValue(){
                LongBinaryOperator LongBinaryOperator=new LongBinaryOperator() {
                        @Override
                        public long applyAsLong(long left, long right) {
                                return left+right;
                        }
                };
                LongAccumulator testObj=new LongAccumulator(LongBinaryOperator,321);
                System.out.println(testObj.floatValue());
        }
        /**
         *返回double值
         * void
         * @Param
         * @author zhqwm
         * @date 2020/6/20 23:39
         */
        @Test
        public void testDoubleValue(){
                LongBinaryOperator LongBinaryOperator=new LongBinaryOperator() {
                        @Override
                        public long applyAsLong(long left, long right) {
                                return left+right;
                        }
                };
                LongAccumulator testObj=new LongAccumulator(LongBinaryOperator,321);
                System.out.println(testObj.doubleValue());
        }

}

 

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