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

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值