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