java.util.concurrent.atomic.AtomicLong#getAndSet()源码实例Demo

下面列出了java.util.concurrent.atomic.AtomicLong#getAndSet() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: smallrye-mutiny   文件: Subscriptions.java
/**
 * 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);
                }
            }
        }
    }
}
 
源代码2 项目: smallrye-mutiny   文件: Subscriptions.java
/**
 * 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;
}
 
源代码3 项目: pmq   文件: MqMetricReporter.java
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;
}
 
源代码4 项目: pmq   文件: MqMetricReporter.java
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;
}
 
源代码5 项目: pmq   文件: MqMetricReporter.java
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;
}
 
源代码6 项目: pmq   文件: MqMetricReporter.java
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;
}
 
源代码7 项目: pmq   文件: MqMetricReporter.java
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;
}
 
源代码8 项目: micrometer   文件: StepValueTest.java
@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);
}
 
源代码9 项目: buffer-slayer   文件: InMemoryReporterMetrics.java
@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);
}
 
源代码10 项目: ignite   文件: ProgressWatchdog.java
/**
 * @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;
}
 
源代码11 项目: disthene   文件: IndexService.java
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);
    }
}
 
源代码12 项目: spectator   文件: SpectatorReporter.java
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);
}
 
private static long deltaAndSet(AtomicLong currentValue, long newValue)
{
    return newValue - currentValue.getAndSet(newValue);
}
 
源代码14 项目: spectator   文件: DoubleDistributionSummary.java
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;
}
 
源代码15 项目: lucene-solr   文件: TestJsonFacets.java
/** 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);
}
 
源代码16 项目: lucene-solr   文件: TestJsonFacets.java
/** 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);
}
 
源代码17 项目: spectator   文件: DoubleDistributionSummary.java
private double toDouble(AtomicLong num, boolean reset) {
  final long v = reset ? num.getAndSet(ZERO) : num.get();
  return Double.longBitsToDouble(v);
}
 
源代码18 项目: spectator   文件: DoubleDistributionSummary.java
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;
}
 
源代码19 项目: aeron   文件: ClusterTimerTest.java
@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();
}
 
/**
 * @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");
}