Java Code Examples for java.util.concurrent.atomic.LongAdder#increment()

The following examples show how to use java.util.concurrent.atomic.LongAdder#increment() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FluxSpecTests.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@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);
	}
 
Example 2
Source File: MonotonicCounterTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 3
Source File: WorkThreadTrackingTaskTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 4
Source File: DefaultApplicationRegistrator.java    From spring-boot-admin with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 5
Source File: LongAdderTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * longValue returns current value.
 */
public void testLongValue() {
    LongAdder ai = new LongAdder();
    assertEquals(0, ai.longValue());
    ai.increment();
    assertEquals(1, ai.longValue());
}
 
Example 6
Source File: LongAdderTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * floatValue returns current value.
 */
public void testFloatValue() {
    LongAdder ai = new LongAdder();
    assertEquals(0.0f, ai.floatValue());
    ai.increment();
    assertEquals(1.0f, ai.floatValue());
}
 
Example 7
Source File: LongAdderTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * toString returns current value.
 */
public void testToString() {
    LongAdder ai = new LongAdder();
    assertEquals("0", ai.toString());
    ai.increment();
    assertEquals(Long.toString(1), ai.toString());
}
 
Example 8
Source File: LongAdderDemo.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    LongAdder a = adder;
    for (int i = 0; i < incs; ++i)
        a.increment();
    result = a.sum();
    phaser.arrive();
}
 
Example 9
Source File: FluxBufferPredicateTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 10
Source File: LongAdderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * doubleValue returns current value.
 */
public void testDoubleValue() {
    LongAdder ai = new LongAdder();
    assertEquals(0.0, ai.doubleValue());
    ai.increment();
    assertEquals(1.0, ai.doubleValue());
}
 
Example 11
Source File: LongAdderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * floatValue returns current value.
 */
public void testFloatValue() {
    LongAdder ai = new LongAdder();
    assertEquals(0.0f, ai.floatValue());
    ai.increment();
    assertEquals(1.0f, ai.floatValue());
}
 
Example 12
Source File: LongAdderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * longValue returns current value.
 */
public void testLongValue() {
    LongAdder ai = new LongAdder();
    assertEquals(0, ai.longValue());
    ai.increment();
    assertEquals(1, ai.longValue());
}
 
Example 13
Source File: LongAdderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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());
}
 
Example 14
Source File: LongAdderDemo.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    LongAdder a = adder;
    for (int i = 0; i < incs; ++i)
        a.increment();
    result = a.sum();
    phaser.arrive();
}
 
Example 15
Source File: ThrowingBiConsumerTest.java    From throwing-function with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 16
Source File: VertxPoolMetrics.java    From vertx-micrometer-metrics with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 17
Source File: LongAdderTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
}
 
Example 18
Source File: LongAdderDemo.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    LongAdder a = adder;
    for (int i = 0; i < incs; ++i)
        a.increment();
    result = a.sum();
    phaser.arrive();
}
 
Example 19
Source File: LongAdderDemo.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    LongAdder a = adder;
    for (int i = 0; i < incs; ++i)
        a.increment();
    result = a.sum();
    phaser.arrive();
}
 
Example 20
Source File: KeyDistributedExecutor.java    From threadly with Mozilla Public License 2.0 4 votes vote down vote up
protected StatisticWorker(Object mapKey, Runnable firstTask) {
  super(mapKey, firstTask);
  
  queueSize = new LongAdder();
  queueSize.increment();
}