下面列出了java.util.concurrent.atomic.LongAdder#increment() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void fluxCanReturnValueAtCertainIndex() {
// "A Flux can return a value at a certain index"
// given: "a composable with values 1 to 5"
Flux<Integer> s = Flux.just(1, 2, 3, 4, 5);
LongAdder error = new LongAdder();
Consumer<Throwable> errorConsumer = e -> error.increment();
// when: "element at index 2 is requested"
Integer tap = s.elementAt(2)
.block();
// then: "3 is emitted"
assertThat(tap).isEqualTo(3);
// when: "element with negative index is requested"
// then: "error is thrown"
assertThatExceptionOfType(IndexOutOfBoundsException.class)
.isThrownBy(() -> s.elementAt(-1));
// when: "element with index > number of values is requested"
// then: "error is thrown"
assertThatExceptionOfType(IndexOutOfBoundsException.class)
.isThrownBy(() -> s.elementAt(10).doOnError(errorConsumer).block());
assertThat(error.intValue()).isEqualTo(1);
}
@Test
public void completeOnlyOnceWithMultipleThread() throws InterruptedException {
LongAdder invocationCount = new LongAdder();
WorkThreadTrackingTask<Object> workThreadTrackingTask =
new WorkThreadTrackingTask<>(
() -> {
invocationCount.increment();
return null;
});
Thread t = new Thread(() -> workThreadTrackingTask.externalCompute());
workThreadTrackingTask.externalCompute();
t.join();
assertEquals(1, invocationCount.intValue());
}
/**
* Registers the client application at spring-boot-admin-server.
* @return true if successful registration on at least one admin server
*/
@Override
public boolean register() {
Application application = this.applicationFactory.createApplication();
boolean isRegistrationSuccessful = false;
for (String adminUrl : this.adminUrls) {
LongAdder attempt = this.attempts.computeIfAbsent(adminUrl, (k) -> new LongAdder());
boolean successful = register(application, adminUrl, attempt.intValue() == 0);
if (!successful) {
attempt.increment();
}
else {
attempt.reset();
isRegistrationSuccessful = true;
if (this.registerOnce) {
break;
}
}
}
return isRegistrationSuccessful;
}
@Test
public void usingLongAdder() {
LongAdder count = new LongAdder();
LongAdder c = PolledMeter.using(registry).withId(id).monitorMonotonicCounter(count);
Assertions.assertSame(count, c);
Counter counter = registry.counter(id);
update();
Assertions.assertEquals(0L, counter.count());
c.increment();
update();
Assertions.assertEquals(1L, counter.count());
c.add(42);
update();
Assertions.assertEquals(43L, counter.count());
}
public void run() {
phaser.arriveAndAwaitAdvance();
phaser.arriveAndAwaitAdvance();
LongAdder a = adder;
for (int i = 0; i < incs; ++i)
a.increment();
result = a.sum();
phaser.arrive();
}
public void run() {
phaser.arriveAndAwaitAdvance();
phaser.arriveAndAwaitAdvance();
LongAdder a = adder;
for (int i = 0; i < incs; ++i)
a.increment();
result = a.sum();
phaser.arrive();
}
/**
* incrementAndGet increments and returns current value
*/
public void testIncrementAndsum() {
LongAdder ai = new LongAdder();
ai.increment();
assertEquals(1, ai.sum());
ai.increment();
assertEquals(2, ai.sum());
}
@Override
public Timers.EventTiming begin(Timers.EventTiming submitted) {
queueSize.get(poolType, poolName).decrement();
submitted.end(poolType, poolName);
LongAdder l = inUse.get(poolType, poolName);
l.increment();
checkRatio(l.longValue());
return usage.start();
}
@Test
void shouldConsume() throws Exception {
// given
LongAdder input = new LongAdder();
ThrowingBiConsumer<Integer, Integer, Exception> consumer = (i, j) -> input.increment();
// when
consumer.accept(2, 3);
// then
assertThat(input.sum()).isEqualTo(1);
}
public void run() {
phaser.arriveAndAwaitAdvance();
phaser.arriveAndAwaitAdvance();
LongAdder a = adder;
for (int i = 0; i < incs; ++i)
a.increment();
result = a.sum();
phaser.arrive();
}
/**
* incrementAndGet increments and returns current value
*/
public void testIncrementAndsum() {
LongAdder ai = new LongAdder();
ai.increment();
assertEquals(1, ai.sum());
ai.increment();
assertEquals(2, ai.sum());
}
/**
* longValue returns current value.
*/
public void testLongValue() {
LongAdder ai = new LongAdder();
assertEquals(0, ai.longValue());
ai.increment();
assertEquals(1, ai.longValue());
}
/**
* longValue returns current value.
*/
public void testLongValue() {
LongAdder ai = new LongAdder();
assertEquals(0, ai.longValue());
ai.increment();
assertEquals(1, ai.longValue());
}
/**
* floatValue returns current value.
*/
public void testFloatValue() {
LongAdder ai = new LongAdder();
assertEquals(0.0f, ai.floatValue());
ai.increment();
assertEquals(1.0f, ai.floatValue());
}
/**
* doubleValue returns current value.
*/
public void testDoubleValue() {
LongAdder ai = new LongAdder();
assertEquals(0.0, ai.doubleValue());
ai.increment();
assertEquals(1.0, ai.doubleValue());
}
@Test
@SuppressWarnings("unchecked")
public void multipleTriggersOfEmptyBufferKeepInitialBuffer() {
//this is best demonstrated with bufferWhile:
FluxIdentityProcessor<Integer> sp1 = Processors.more().multicastNoBackpressure();
LongAdder bufferCount = new LongAdder();
Supplier<List<Integer>> bufferSupplier = () -> {
bufferCount.increment();
return new ArrayList<>();
};
FluxBufferPredicate<Integer, List<Integer>> bufferWhile = new
FluxBufferPredicate<>(
sp1, i -> i >= 10,
bufferSupplier,
FluxBufferPredicate.Mode.WHILE);
StepVerifier.create(bufferWhile)
.then(() -> assertThat(bufferCount.intValue()).isOne())
.then(() -> sp1.onNext(1))
.then(() -> sp1.onNext(2))
.then(() -> sp1.onNext(3))
.then(() -> assertThat(bufferCount.intValue()).isOne())
.expectNoEvent(Duration.ofMillis(10))
.then(() -> sp1.onNext(10))
.then(() -> sp1.onNext(11))
.then(sp1::onComplete)
.expectNext(Arrays.asList(10, 11))
.then(() -> assertThat(bufferCount.intValue()).isOne())
.expectComplete()
.verify();
assertThat(bufferCount.intValue()).isOne();
}
public void run() {
phaser.arriveAndAwaitAdvance();
phaser.arriveAndAwaitAdvance();
LongAdder a = adder;
for (int i = 0; i < incs; ++i)
a.increment();
result = a.sum();
phaser.arrive();
}
/**
* toString returns current value.
*/
public void testToString() {
LongAdder ai = new LongAdder();
assertEquals("0", ai.toString());
ai.increment();
assertEquals(Long.toString(1), ai.toString());
}
/**
* floatValue returns current value.
*/
public void testFloatValue() {
LongAdder ai = new LongAdder();
assertEquals(0.0f, ai.floatValue());
ai.increment();
assertEquals(1.0f, ai.floatValue());
}
protected StatisticWorker(Object mapKey, Runnable firstTask) {
super(mapKey, firstTask);
queueSize = new LongAdder();
queueSize.increment();
}