下面列出了怎么用java.util.concurrent.atomic.DoubleAccumulator的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* accumulates by multiple threads produce correct result
*/
public void testAccumulateAndGetMT() {
final int incs = 1000000;
final int nthreads = 4;
final ExecutorService pool = Executors.newCachedThreadPool();
DoubleAccumulator a = new DoubleAccumulator(Double::max, 0.0);
Phaser phaser = new Phaser(nthreads + 1);
for (int i = 0; i < nthreads; ++i)
pool.execute(new AccTask(a, phaser, incs));
phaser.arriveAndAwaitAdvance();
phaser.arriveAndAwaitAdvance();
double expected = incs - 1;
double result = a.get();
assertEquals(expected, result);
pool.shutdown();
}
protected SkywalkingDistributionSummary(Id id, MeterId meterId, SkywalkingConfig config, Clock clock,
DistributionStatisticConfig distributionStatisticConfig, double scale,
boolean supportsAggregablePercentiles) {
super(id, clock, distributionStatisticConfig, scale, supportsAggregablePercentiles);
// meter base name
String baseName = meterId.getName();
this.counter = MeterBuilder.buildCounter(meterId.copyTo(baseName + "_count", MeterId.MeterType.COUNTER), config);
this.sum = MeterBuilder.buildCounter(meterId.copyTo(baseName + "_sum", MeterId.MeterType.COUNTER), config);
this.maxAdder = new DoubleAccumulator((a, b) -> a > b ? a : b, 0.000);
this.max = MeterFactory.gauge(meterId.copyTo(baseName + "_max", MeterId.MeterType.GAUGE),
() -> maxAdder.doubleValue()).build();
this.histogram = MeterBuilder.buildHistogram(meterId, supportsAggregablePercentiles, distributionStatisticConfig, false);
}
protected SkywalkingTimer(Id id, MeterId meterId, SkywalkingConfig config, Clock clock,
DistributionStatisticConfig distributionStatisticConfig, PauseDetector pauseDetector,
TimeUnit baseTimeUnit, boolean supportsAggregablePercentiles) {
super(id, clock, distributionStatisticConfig, pauseDetector, baseTimeUnit, supportsAggregablePercentiles);
// meter base name
String baseName = meterId.getName();
this.counter = MeterBuilder.buildCounter(meterId.copyTo(baseName + "_count", MeterId.MeterType.COUNTER), config);
this.sum = MeterBuilder.buildCounter(meterId.copyTo(baseName + "_sum", MeterId.MeterType.COUNTER), config);
this.maxAdder = new DoubleAccumulator((a, b) -> a > b ? a : b, 0.000);
this.max = MeterFactory.gauge(meterId.copyTo(baseName + "_max", MeterId.MeterType.GAUGE),
() -> maxAdder.doubleValue()).build();
this.histogram = MeterBuilder.buildHistogram(meterId, supportsAggregablePercentiles, distributionStatisticConfig, true);
}
/**
* accumulates by multiple threads produce correct result
*/
public void testAccumulateAndGetMT() {
final int incs = 1000000;
final int nthreads = 4;
final ExecutorService pool = Executors.newCachedThreadPool();
DoubleAccumulator a = new DoubleAccumulator(Double::max, 0.0);
Phaser phaser = new Phaser(nthreads + 1);
for (int i = 0; i < nthreads; ++i)
pool.execute(new AccTask(a, phaser, incs));
phaser.arriveAndAwaitAdvance();
phaser.arriveAndAwaitAdvance();
double expected = incs - 1;
double result = a.get();
assertEquals(expected, result);
pool.shutdown();
}
static void testDoubleAccumulator() {
DoubleBinaryOperator plus = (DoubleBinaryOperator & Serializable) (x, y) -> x + y;
DoubleAccumulator a = new DoubleAccumulator(plus, 13.9d);
a.accumulate(17.5d);
DoubleAccumulator result = echo(a);
if (result.get() != a.get())
throw new RuntimeException("Unexpected value");
a.reset();
result.reset();
if (result.get() != a.get())
throw new RuntimeException("Unexpected value after reset");
checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAccumulator$SerializationProxy");
}
static void testDoubleAccumulator() {
DoubleBinaryOperator plus = (DoubleBinaryOperator & Serializable) (x, y) -> x + y;
DoubleAccumulator a = new DoubleAccumulator(plus, 13.9d);
a.accumulate(17.5d);
DoubleAccumulator result = echo(a);
if (result.get() != a.get())
throw new RuntimeException("Unexpected value");
a.reset();
result.reset();
if (result.get() != a.get())
throw new RuntimeException("Unexpected value after reset");
checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAccumulator$SerializationProxy");
}
static void testDoubleAccumulator() {
DoubleBinaryOperator plus = (DoubleBinaryOperator & Serializable) (x, y) -> x + y;
DoubleAccumulator a = new DoubleAccumulator(plus, 13.9d);
a.accumulate(17.5d);
DoubleAccumulator result = echo(a);
if (result.get() != a.get())
throw new RuntimeException("Unexpected value");
a.reset();
result.reset();
if (result.get() != a.get())
throw new RuntimeException("Unexpected value after reset");
checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAccumulator$SerializationProxy");
}
static void testDoubleAccumulator() {
DoubleBinaryOperator plus = (DoubleBinaryOperator & Serializable) (x, y) -> x + y;
DoubleAccumulator a = new DoubleAccumulator(plus, 13.9d);
a.accumulate(17.5d);
DoubleAccumulator result = echo(a);
if (result.get() != a.get())
throw new RuntimeException("Unexpected value");
a.reset();
result.reset();
if (result.get() != a.get())
throw new RuntimeException("Unexpected value after reset");
checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAccumulator$SerializationProxy");
}
static void testDoubleAccumulator() {
DoubleBinaryOperator plus = (DoubleBinaryOperator & Serializable) (x, y) -> x + y;
DoubleAccumulator a = new DoubleAccumulator(plus, 13.9d);
a.accumulate(17.5d);
DoubleAccumulator result = echo(a);
if (result.get() != a.get())
throw new RuntimeException("Unexpected value");
a.reset();
result.reset();
if (result.get() != a.get())
throw new RuntimeException("Unexpected value after reset");
checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAccumulator$SerializationProxy");
}
static void testDoubleAccumulator() {
DoubleBinaryOperator plus = (DoubleBinaryOperator & Serializable) (x, y) -> x + y;
DoubleAccumulator a = new DoubleAccumulator(plus, 13.9d);
a.accumulate(17.5d);
DoubleAccumulator result = echo(a);
if (result.get() != a.get())
throw new RuntimeException("Unexpected value");
a.reset();
result.reset();
if (result.get() != a.get())
throw new RuntimeException("Unexpected value after reset");
checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAccumulator$SerializationProxy");
}
static void testDoubleAccumulator() {
DoubleBinaryOperator plus = (DoubleBinaryOperator & Serializable) (x, y) -> x + y;
DoubleAccumulator a = new DoubleAccumulator(plus, 13.9d);
a.accumulate(17.5d);
DoubleAccumulator result = echo(a);
if (result.get() != a.get())
throw new RuntimeException("Unexpected value");
a.reset();
result.reset();
if (result.get() != a.get())
throw new RuntimeException("Unexpected value after reset");
checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAccumulator$SerializationProxy");
}
/**
* accumulate accumulates given value to current, and get returns current value
*/
public void testAccumulateAndGet() {
DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
ai.accumulate(2.0);
assertEquals(2.0, ai.get());
ai.accumulate(-4.0);
assertEquals(2.0, ai.get());
ai.accumulate(4.0);
assertEquals(4.0, ai.get());
}
/**
* reset() causes subsequent get() to return zero
*/
public void testReset() {
DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
ai.accumulate(2.0);
assertEquals(2.0, ai.get());
ai.reset();
assertEquals(0.0, ai.get());
}
/**
* getThenReset() returns current value; subsequent get() returns zero
*/
public void testGetThenReset() {
DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
ai.accumulate(2.0);
assertEquals(2.0, ai.get());
assertEquals(2.0, ai.getThenReset());
assertEquals(0.0, ai.get());
}
/**
* toString returns current value.
*/
public void testToString() {
DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
assertEquals("0.0", ai.toString());
ai.accumulate(1.0);
assertEquals(Double.toString(1.0), ai.toString());
}
/**
* intValue returns current value.
*/
public void testIntValue() {
DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
assertEquals(0, ai.intValue());
ai.accumulate(1.0);
assertEquals(1, ai.intValue());
}
/**
* longValue returns current value.
*/
public void testLongValue() {
DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
assertEquals(0, ai.longValue());
ai.accumulate(1.0);
assertEquals(1, ai.longValue());
}
/**
* floatValue returns current value.
*/
public void testFloatValue() {
DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
assertEquals(0.0f, ai.floatValue());
ai.accumulate(1.0);
assertEquals(1.0f, ai.floatValue());
}
/**
* doubleValue returns current value.
*/
public void testDoubleValue() {
DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
assertEquals(0.0, ai.doubleValue());
ai.accumulate(1.0);
assertEquals(1.0, ai.doubleValue());
}
public void run() {
phaser.arriveAndAwaitAdvance();
DoubleAccumulator a = acc;
for (int i = 0; i < incs; ++i)
a.accumulate(i);
result = a.get();
phaser.arrive();
}
static void testDoubleAccumulator() {
DoubleBinaryOperator plus = (DoubleBinaryOperator & Serializable) (x, y) -> x + y;
DoubleAccumulator a = new DoubleAccumulator(plus, 13.9d);
a.accumulate(17.5d);
DoubleAccumulator result = echo(a);
if (result.get() != a.get())
throw new RuntimeException("Unexpected value");
a.reset();
result.reset();
if (result.get() != a.get())
throw new RuntimeException("Unexpected value after reset");
checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAccumulator$SerializationProxy");
}
static void testDoubleAccumulator() {
DoubleBinaryOperator plus = (DoubleBinaryOperator & Serializable) (x, y) -> x + y;
DoubleAccumulator a = new DoubleAccumulator(plus, 13.9d);
a.accumulate(17.5d);
DoubleAccumulator result = echo(a);
if (result.get() != a.get())
throw new RuntimeException("Unexpected value");
a.reset();
result.reset();
if (result.get() != a.get())
throw new RuntimeException("Unexpected value after reset");
checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAccumulator$SerializationProxy");
}
static void testDoubleAccumulator() {
DoubleBinaryOperator plus = (DoubleBinaryOperator & Serializable) (x, y) -> x + y;
DoubleAccumulator a = new DoubleAccumulator(plus, 13.9d);
a.accumulate(17.5d);
DoubleAccumulator result = echo(a);
if (result.get() != a.get())
throw new RuntimeException("Unexpected value");
a.reset();
result.reset();
if (result.get() != a.get())
throw new RuntimeException("Unexpected value after reset");
checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAccumulator$SerializationProxy");
}
static void testDoubleAccumulator() {
DoubleBinaryOperator plus = (DoubleBinaryOperator & Serializable) (x, y) -> x + y;
DoubleAccumulator a = new DoubleAccumulator(plus, 13.9d);
a.accumulate(17.5d);
DoubleAccumulator result = echo(a);
if (result.get() != a.get())
throw new RuntimeException("Unexpected value");
a.reset();
result.reset();
if (result.get() != a.get())
throw new RuntimeException("Unexpected value after reset");
checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAccumulator$SerializationProxy");
}
static void testDoubleAccumulator() {
DoubleBinaryOperator plus = (DoubleBinaryOperator & Serializable) (x, y) -> x + y;
DoubleAccumulator a = new DoubleAccumulator(plus, 13.9d);
a.accumulate(17.5d);
DoubleAccumulator result = echo(a);
if (result.get() != a.get())
throw new RuntimeException("Unexpected value");
a.reset();
result.reset();
if (result.get() != a.get())
throw new RuntimeException("Unexpected value after reset");
checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAccumulator$SerializationProxy");
}
static void testDoubleAccumulator() {
DoubleBinaryOperator plus = (DoubleBinaryOperator & Serializable) (x, y) -> x + y;
DoubleAccumulator a = new DoubleAccumulator(plus, 13.9d);
a.accumulate(17.5d);
DoubleAccumulator result = echo(a);
if (result.get() != a.get())
throw new RuntimeException("Unexpected value");
a.reset();
result.reset();
if (result.get() != a.get())
throw new RuntimeException("Unexpected value after reset");
checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAccumulator$SerializationProxy");
}
static void testDoubleAccumulator() {
DoubleBinaryOperator plus = (DoubleBinaryOperator & Serializable) (x, y) -> x + y;
DoubleAccumulator a = new DoubleAccumulator(plus, 13.9d);
a.accumulate(17.5d);
DoubleAccumulator result = echo(a);
if (result.get() != a.get())
throw new RuntimeException("Unexpected value");
a.reset();
result.reset();
if (result.get() != a.get())
throw new RuntimeException("Unexpected value after reset");
checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAccumulator$SerializationProxy");
}
/**
* accumulate accumulates given value to current, and get returns current value
*/
public void testAccumulateAndGet() {
DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
ai.accumulate(2.0);
assertEquals(2.0, ai.get());
ai.accumulate(-4.0);
assertEquals(2.0, ai.get());
ai.accumulate(4.0);
assertEquals(4.0, ai.get());
}
/**
* reset() causes subsequent get() to return zero
*/
public void testReset() {
DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
ai.accumulate(2.0);
assertEquals(2.0, ai.get());
ai.reset();
assertEquals(0.0, ai.get());
}
/**
* getThenReset() returns current value; subsequent get() returns zero
*/
public void testGetThenReset() {
DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0);
ai.accumulate(2.0);
assertEquals(2.0, ai.get());
assertEquals(2.0, ai.getThenReset());
assertEquals(0.0, ai.get());
}