下面列出了java.util.concurrent.atomic.AtomicLong#getAndSet() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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);
}
}
}
}
}
/**
* 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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
@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);
}
@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);
}
/**
* @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;
}
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);
}
}
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);
}
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;
}
/** 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);
}
/** 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);
}
private double toDouble(AtomicLong num, boolean reset) {
final long v = reset ? num.getAndSet(ZERO) : num.get();
return Double.longBitsToDouble(v);
}
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;
}
@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");
}