Java Code Examples for java.util.concurrent.atomic.AtomicLong#getAndSet()

The following examples show how to use java.util.concurrent.atomic.AtomicLong#getAndSet() . These examples are extracted from open source projects. 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 Project: smallrye-mutiny   File: Subscriptions.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Atomically requests from the Subscription in the field if not null, otherwise accumulates
 * the request amount in the requested field to be requested once the field is set to non-null.
 *
 * @param field the target field that may already contain a Subscription
 * @param requested the current requested amount
 * @param requests the request amount, positive (verified)
 */
public static void requestIfNotNullOrAccumulate(AtomicReference<Subscription> field, AtomicLong requested, long requests) {
    Subscription subscription = field.get();
    if (subscription != null) {
        subscription.request(requests);
    } else {
        if (requests > 0) {
            add(requested, requests);
            subscription = field.get();
            if (subscription != null) {
                long r = requested.getAndSet(0L);
                if (r != 0L) {
                    subscription.request(r);
                }
            }
        }
    }
}
 
Example 2
Source Project: smallrye-mutiny   File: Subscriptions.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Atomically sets the new {@link Subscription} in the container and requests any accumulated amount
 * from the requested counter.
 *
 * @param container the target field for the new Subscription
 * @param requested the current requested amount
 * @param subscription the new Subscription, must not be {@code null}
 * @return true if the Subscription was set the first time
 */
public static boolean setIfEmptyAndRequest(AtomicReference<Subscription> container, AtomicLong requested,
        Subscription subscription) {
    if (Subscriptions.setIfEmpty(container, subscription)) {
        long r = requested.getAndSet(0L);
        if (r > 0L) {
            subscription.request(r);
        } else if (r < 0) {
            throw new IllegalArgumentException("Invalid amount of request");
        }
        return true;
    }
    return false;
}
 
Example 3
Source Project: pmq   File: MqMetricReporter.java    License: Apache License 2.0 5 votes vote down vote up
private long getChangeCount(String name, long count) {
	if(!this.lastReport.containsKey(name)){
		this.lastReport.put(name, new AtomicLong(0));
	}
    //this.lastReport.putIfAbsent(name, new AtomicLong(0));
    AtomicLong last = this.lastReport.get(name);
    long lastCount = last.getAndSet(count);
    return count - lastCount;
}
 
Example 4
Source Project: pmq   File: MqMetricReporter.java    License: Apache License 2.0 5 votes vote down vote up
private long getChangeCount(String name, long count) {
	if(!this.lastReport.containsKey(name)){
		this.lastReport.put(name, new AtomicLong(0));
	}
    //this.lastReport.putIfAbsent(name, new AtomicLong(0));
    AtomicLong last = this.lastReport.get(name);
    long lastCount = last.getAndSet(count);
    return count - lastCount;
}
 
Example 5
Source Project: pmq   File: MqMetricReporter.java    License: Apache License 2.0 5 votes vote down vote up
private long getChangeCount(String name, long count) {
	if(!this.lastReport.containsKey(name)){
		this.lastReport.put(name, new AtomicLong(0));
	}
    //this.lastReport.putIfAbsent(name, new AtomicLong(0));
    AtomicLong last = this.lastReport.get(name);
    long lastCount = last.getAndSet(count);
    return count - lastCount;
}
 
Example 6
Source Project: pmq   File: MqMetricReporter.java    License: Apache License 2.0 5 votes vote down vote up
private long getChangeCount(String name, long count) {
	if(!this.lastReport.containsKey(name)){
		this.lastReport.put(name, new AtomicLong(0));
	}
	//this.lastReport.putIfAbsent(name, new AtomicLong(0));
	AtomicLong last = this.lastReport.get(name);
	long lastCount = last.getAndSet(count);
	return count - lastCount;
}
 
Example 7
Source Project: pmq   File: MqMetricReporter.java    License: Apache License 2.0 5 votes vote down vote up
private long getChangeCount(String name, long count) {
	if(!this.lastReport.containsKey(name)){
		this.lastReport.put(name, new AtomicLong(0));
	}
    //this.lastReport.putIfAbsent(name, new AtomicLong(0));
    AtomicLong last = this.lastReport.get(name);
    long lastCount = last.getAndSet(count);
    return count - lastCount;
}
 
Example 8
Source Project: micrometer   File: StepValueTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void poll() {
    final AtomicLong aLong = new AtomicLong(42);
    final long stepTime = 60;

    final StepValue<Long> stepValue = new StepValue<Long>(clock, stepTime) {
        @Override
        public Supplier<Long> valueSupplier() {
            return () -> aLong.getAndSet(0);
        }

        @Override
        public Long noValue() {
            return 0L;
        }
    };

    assertThat(stepValue.poll()).isEqualTo(0L);

    clock.add(Duration.ofMillis(1));
    assertThat(stepValue.poll()).isEqualTo(0L);

    clock.add(Duration.ofMillis(59));
    assertThat(stepValue.poll()).isEqualTo(42L);

    clock.add(Duration.ofMillis(60));
    assertThat(stepValue.poll()).isEqualTo(0L);

    clock.add(Duration.ofMillis(60));
    assertThat(stepValue.poll()).isEqualTo(0L);

    aLong.set(24);
    assertThat(stepValue.poll()).isEqualTo(0L);

    clock.add(Duration.ofMillis(60));
    assertThat(stepValue.poll()).isEqualTo(24L);
}
 
Example 9
@Override
public void updateQueuedMessages(MessageKey key, int update) {
  AtomicLong metric = queuedMessages.get(key);
  if (metric == null) {
    metric = queuedMessages.putIfAbsent(key, new AtomicLong(update));
    if (metric == null) {
      queuedMessagesAccumulator.add(update);
      return;
    }
  }
  long prev = metric.getAndSet(update);
  queuedMessagesAccumulator.add(update - prev);
}
 
Example 10
Source Project: ignite   File: ProgressWatchdog.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @param elapsedMsFromPrevTick time from previous tick, millis.
 * @param absVal current value
 * @param cnt counter stores previous value.
 * @return value change from previous tick.
 */
private long detectDelta(long elapsedMsFromPrevTick, long absVal, AtomicLong cnt) {
    long cpPagesChange = absVal - cnt.getAndSet(absVal);

    if (cpPagesChange < 0)
        cpPagesChange = 0;

    return (cpPagesChange * 1000) / elapsedMsFromPrevTick;
}
 
Example 11
Source Project: disthene   File: IndexService.java    License: MIT License 5 votes vote down vote up
private void handleWithCache(Metric metric) {
    ConcurrentMap<String, AtomicLong> tenantPaths = getTenantPaths(metric.getTenant());
    AtomicLong lastSeen = tenantPaths.get(metric.getPath());

    if (lastSeen == null) {
        lastSeen = tenantPaths.putIfAbsent(metric.getPath(), new AtomicLong(System.currentTimeMillis() / 1000L));
        if (lastSeen == null) {
            metrics.offer(metric);
        } else {
            lastSeen.getAndSet(System.currentTimeMillis() / 1000L);
        }
    } else {
        lastSeen.getAndSet(System.currentTimeMillis() / 1000L);
    }
}
 
Example 12
Source Project: spectator   File: SpectatorReporter.java    License: Apache License 2.0 5 votes vote down vote up
private long getAndSetPrevious(String name, long newValue) {
  AtomicLong prev = previousValues.get(name);
  if (prev == null) {
    AtomicLong tmp = new AtomicLong(0L);
    prev = previousValues.putIfAbsent(name, tmp);
    prev = (prev == null) ? tmp : prev;
  }
  return prev.getAndSet(newValue);
}
 
Example 13
private static long deltaAndSet(AtomicLong currentValue, long newValue)
{
    return newValue - currentValue.getAndSet(newValue);
}
 
Example 14
private double toRateDouble(AtomicLong num, long deltaMillis, boolean reset) {
  final long v = reset ? num.getAndSet(ZERO) : num.get();
  final double delta = deltaMillis / 1000.0;
  return Double.longBitsToDouble(v) / delta;
}
 
Example 15
Source Project: lucene-solr   File: TestJsonFacets.java    License: Apache License 2.0 4 votes vote down vote up
/** atomicly resets the acctual AtomicLong value matches the expected and resets it to  0 */
private static final void assertEqualsAndReset(String msg, long expected, AtomicLong actual) {
  final long current = actual.getAndSet(0);
  assertEquals(msg, expected, current);
}
 
Example 16
Source Project: lucene-solr   File: TestJsonFacets.java    License: Apache License 2.0 4 votes vote down vote up
/** atomicly resets the acctual AtomicLong value matches the expected and resets it to  0 */
private static final void assertEqualsAndReset(long expected, AtomicLong actual) {
  final long current = actual.getAndSet(0);
  assertEquals(expected, current);
}
 
Example 17
private double toDouble(AtomicLong num, boolean reset) {
  final long v = reset ? num.getAndSet(ZERO) : num.get();
  return Double.longBitsToDouble(v);
}
 
Example 18
private double toRateLong(AtomicLong num, long deltaMillis, boolean reset) {
  final long v = reset ? num.getAndSet(0L) : num.get();
  final double delta = deltaMillis / 1000.0;
  return v / delta;
}
 
Example 19
Source Project: aeron   File: ClusterTimerTest.java    License: Apache License 2.0 3 votes vote down vote up
@Test
@Timeout(10)
public void shouldTriggerRescheduledTimerAfterReplay()
{
    final AtomicLong triggeredTimersCounter = new AtomicLong();

    launchReschedulingService(triggeredTimersCounter);
    connectClient();

    Tests.awaitValue(triggeredTimersCounter, 2);

    forceCloseForRestart();

    long triggeredSinceStart = triggeredTimersCounter.getAndSet(0);

    launchClusteredMediaDriver(false);
    launchReschedulingService(triggeredTimersCounter);

    Tests.awaitValue(triggeredTimersCounter, triggeredSinceStart + 2);

    forceCloseForRestart();

    triggeredSinceStart = triggeredTimersCounter.getAndSet(0);

    launchClusteredMediaDriver(false);
    launchReschedulingService(triggeredTimersCounter);

    Tests.awaitValue(triggeredTimersCounter, triggeredSinceStart + 4);

    ClusterTests.failOnClusterError();
}
 
Example 20
/**
 * @throws Exception If failed.
 */
private void checkPuts(int threadCnt, long duration) throws Exception {
    final AtomicLong opCnt = new AtomicLong();
    final AtomicLong totalOpCnt = new AtomicLong();

    final AtomicBoolean done = new AtomicBoolean();

    long start = System.currentTimeMillis();

    IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {
        @Override public Object call() throws Exception {
            Random rnd = new Random();

            byte[] val = new byte[1024];

            long locTotalOpCnt = 0;

            while (!done.get()) {
                for (int i = 0; i < 500; i++) {
                    T3<Integer, Integer, byte[]> key = randomKey(rnd);

                    map.put(key.get1(), key.get2(), key.get3(), val);
                }

                locTotalOpCnt += 500;
                opCnt.addAndGet(500);
            }

            totalOpCnt.addAndGet(locTotalOpCnt);

            return null;
        }
    }, threadCnt);

    final int step = 2000;

    while (System.currentTimeMillis() - start < duration) {
        U.sleep(step);

        long ops = opCnt.getAndSet(0);

        info("Putting " + (ops * 1000) / step + " ops/sec");
    }

    done.set(true);

    fut.get();

    long end = System.currentTimeMillis();

    info("Average put performance: " + (totalOpCnt.get() * 1000) / (end - start) + " ops/sec");
}