下面列出了怎么用java.util.concurrent.atomic.DoubleAdder的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* adds by multiple threads produce correct sum
*/
public void testAddAndSumMT() throws Throwable {
final int incs = 1000000;
final int nthreads = 4;
final ExecutorService pool = Executors.newCachedThreadPool();
DoubleAdder a = new DoubleAdder();
CyclicBarrier barrier = new CyclicBarrier(nthreads + 1);
for (int i = 0; i < nthreads; ++i)
pool.execute(new AdderTask(a, barrier, incs));
barrier.await();
barrier.await();
double total = (long)nthreads * incs;
double sum = a.sum();
assertEquals(sum, total);
pool.shutdown();
}
static void adderTest(int nthreads, int incs) {
System.out.print("DoubleAdder ");
Phaser phaser = new Phaser(nthreads + 1);
DoubleAdder a = new DoubleAdder();
for (int i = 0; i < nthreads; ++i)
pool.execute(new AdderTask(a, phaser, incs));
report(nthreads, incs, timeTasks(phaser), a.sum());
}
public void run() {
phaser.arriveAndAwaitAdvance();
phaser.arriveAndAwaitAdvance();
DoubleAdder a = adder;
for (int i = 0; i < incs; ++i)
a.add(1.0);
result = a.sum();
phaser.arrive();
}
static void testDoubleAdder() {
DoubleAdder a = new DoubleAdder();
a.add(20.1d);
DoubleAdder result = echo(a);
if (result.doubleValue() != a.doubleValue())
throw new RuntimeException("Unexpected doubleValue");
checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAdder$SerializationProxy");
}
static void adderTest(int nthreads, int incs) {
System.out.print("DoubleAdder ");
Phaser phaser = new Phaser(nthreads + 1);
DoubleAdder a = new DoubleAdder();
for (int i = 0; i < nthreads; ++i)
pool.execute(new AdderTask(a, phaser, incs));
report(nthreads, incs, timeTasks(phaser), a.sum());
}
public void run() {
phaser.arriveAndAwaitAdvance();
phaser.arriveAndAwaitAdvance();
DoubleAdder a = adder;
for (int i = 0; i < incs; ++i)
a.add(1.0);
result = a.sum();
phaser.arrive();
}
static void testDoubleAdder() {
DoubleAdder a = new DoubleAdder();
a.add(20.1d);
DoubleAdder result = echo(a);
if (result.doubleValue() != a.doubleValue())
throw new RuntimeException("Unexpected doubleValue");
checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAdder$SerializationProxy");
}
/**
* reset() causes subsequent sum() to return zero
*/
public void testReset() {
DoubleAdder ai = new DoubleAdder();
ai.add(2.0);
assertEquals(2.0, ai.sum());
ai.reset();
assertEquals(0.0, ai.sum());
}
public void run() {
phaser.arriveAndAwaitAdvance();
phaser.arriveAndAwaitAdvance();
DoubleAdder a = adder;
for (int i = 0; i < incs; ++i)
a.add(1.0);
result = a.sum();
phaser.arrive();
}
static void testDoubleAdder() {
DoubleAdder a = new DoubleAdder();
a.add(20.1d);
DoubleAdder result = echo(a);
if (result.doubleValue() != a.doubleValue())
throw new RuntimeException("Unexpected doubleValue");
checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAdder$SerializationProxy");
}
static void adderTest(int nthreads, int incs) {
System.out.print("DoubleAdder ");
Phaser phaser = new Phaser(nthreads + 1);
DoubleAdder a = new DoubleAdder();
for (int i = 0; i < nthreads; ++i)
pool.execute(new AdderTask(a, phaser, incs));
report(nthreads, incs, timeTasks(phaser), a.sum());
}
public void run() {
phaser.arriveAndAwaitAdvance();
phaser.arriveAndAwaitAdvance();
DoubleAdder a = adder;
for (int i = 0; i < incs; ++i)
a.add(1.0);
result = a.sum();
phaser.arrive();
}
static void testDoubleAdder() {
DoubleAdder a = new DoubleAdder();
a.add(20.1d);
DoubleAdder result = echo(a);
if (result.doubleValue() != a.doubleValue())
throw new RuntimeException("Unexpected doubleValue");
checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAdder$SerializationProxy");
}
/***************************************
* Main.
*
* @param rArgs
*/
public static void main(String[] rArgs)
{
ThreadLocalRandom rRandom = ThreadLocalRandom.current();
long[] aCoefficients = new long[TASK_SIZE];
for (int i = 0; i < TASK_SIZE; i++)
{
aCoefficients[i] = rRandom.nextLong(0, 1024 * 1024);
}
Profiler aProfiler =
new Profiler(ForkJoinExample.class.getSimpleName());
ScopeFuture<DoubleAdder> aResult =
produce(
RESULT,
scope -> computeOrSplit(scope, aCoefficients, 0, TASK_SIZE));
System.out.printf("Sum: %f\n", aResult.get().sum());
aProfiler.measure(
String.format(
"%d tasks\nwith a batch size of %d",
TASK_SIZE,
BATCH_SIZE));
aProfiler.printSummary();
}
static void adderTest(int nthreads, int incs) {
System.out.print("DoubleAdder ");
Phaser phaser = new Phaser(nthreads + 1);
DoubleAdder a = new DoubleAdder();
for (int i = 0; i < nthreads; ++i)
pool.execute(new AdderTask(a, phaser, incs));
report(nthreads, incs, timeTasks(phaser), a.sum());
}
public void run() {
phaser.arriveAndAwaitAdvance();
phaser.arriveAndAwaitAdvance();
DoubleAdder a = adder;
for (int i = 0; i < incs; ++i)
a.add(1.0);
result = a.sum();
phaser.arrive();
}
static void testDoubleAdder() {
DoubleAdder a = new DoubleAdder();
a.add(20.1d);
DoubleAdder result = echo(a);
if (result.doubleValue() != a.doubleValue())
throw new RuntimeException("Unexpected doubleValue");
checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAdder$SerializationProxy");
}
public FixedBoundaryVictoriaMetricsHistogram() {
this.zeros = new AtomicLong(0);
this.lower = new AtomicLong(0);
this.upper = new AtomicLong(0);
this.sum = new DoubleAdder();
this.values = new AtomicReferenceArray<>(BUCKETS_COUNT);
}
public CumulativeDistributionSummary(Id id, Clock clock, DistributionStatisticConfig distributionStatisticConfig,
double scale, boolean supportsAggregablePercentiles) {
super(id, clock, distributionStatisticConfig, scale, supportsAggregablePercentiles);
this.count = new AtomicLong();
this.total = new DoubleAdder();
this.max = new TimeWindowMax(clock, distributionStatisticConfig);
}
static void adderTest(int nthreads, int incs) {
System.out.print("DoubleAdder ");
Phaser phaser = new Phaser(nthreads + 1);
DoubleAdder a = new DoubleAdder();
for (int i = 0; i < nthreads; ++i)
pool.execute(new AdderTask(a, phaser, incs));
report(nthreads, incs, timeTasks(phaser), a.sum());
}
/**
* floatValue returns current value.
*/
public void testFloatValue() {
DoubleAdder ai = new DoubleAdder();
assertEquals(0.0f, ai.floatValue());
ai.add(1.0);
assertEquals(1.0f, ai.floatValue());
}
public void run() {
phaser.arriveAndAwaitAdvance();
phaser.arriveAndAwaitAdvance();
DoubleAdder a = adder;
for (int i = 0; i < incs; ++i)
a.add(1.0);
result = a.sum();
phaser.arrive();
}
public void run() {
try {
barrier.await();
DoubleAdder a = adder;
for (int i = 0; i < incs; ++i)
a.add(1.0);
result = a.sum();
barrier.await();
} catch (Throwable t) { throw new Error(t); }
}
static void adderTest(int nthreads, int incs) {
System.out.print("DoubleAdder ");
Phaser phaser = new Phaser(nthreads + 1);
DoubleAdder a = new DoubleAdder();
for (int i = 0; i < nthreads; ++i)
pool.execute(new AdderTask(a, phaser, incs));
report(nthreads, incs, timeTasks(phaser), a.sum());
}
/**
* intValue returns current value.
*/
public void testIntValue() {
DoubleAdder ai = new DoubleAdder();
assertEquals(0, ai.intValue());
ai.add(1.0);
assertEquals(1, ai.intValue());
}
static void testDoubleAdder() {
DoubleAdder a = new DoubleAdder();
a.add(20.1d);
DoubleAdder result = echo(a);
if (result.doubleValue() != a.doubleValue())
throw new RuntimeException("Unexpected doubleValue");
checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAdder$SerializationProxy");
}
/**
* reset() causes subsequent sum() to return zero
*/
public void testReset() {
DoubleAdder ai = new DoubleAdder();
ai.add(2.0);
assertEquals(2.0, ai.sum());
ai.reset();
assertEquals(0.0, ai.sum());
}
/**
* sumThenReset() returns sum; subsequent sum() returns zero
*/
public void testSumThenReset() {
DoubleAdder ai = new DoubleAdder();
ai.add(2.0);
assertEquals(2.0, ai.sum());
assertEquals(2.0, ai.sumThenReset());
assertEquals(0.0, ai.sum());
}
/**
* sumThenReset() returns sum; subsequent sum() returns zero
*/
public void testSumThenReset() {
DoubleAdder ai = new DoubleAdder();
ai.add(2.0);
assertEquals(2.0, ai.sum());
assertEquals(2.0, ai.sumThenReset());
assertEquals(0.0, ai.sum());
}
/**
* toString returns current value.
*/
public void testToString() {
DoubleAdder ai = new DoubleAdder();
assertEquals(Double.toString(0.0), ai.toString());
ai.add(1.0);
assertEquals(Double.toString(1.0), ai.toString());
}