idToResources.getAndAccumulate(resources, (orig, update) -> Utils.merge(orig, update)); idToWorkerResources.getAndAccumulate(workerResources, (orig, update) -> Utils.merge(orig, update));
private void drain(Consumer<? super T> action, T[] buf) { T last = buf[idx-1]; T acc = accRef.getAndAccumulate(last, op); if(acc != NONE) { for(int i=0; i<idx; i++) { action.accept(localOp.apply(buf[i], acc)); } } else { for(int i=0; i<idx; i++) { action.accept(buf[i]); } } }
import java.math.BigInteger; import java.util.Objects; import java.util.concurrent.atomic.AtomicReference; public final class AtomicBigInteger { private final AtomicReference<BigInteger> bigInteger; public AtomicBigInteger(final BigInteger bigInteger) { this.bigInteger = new AtomicReference<>(Objects.requireNonNull(bigInteger)); } // Method references left out for demonstration purposes public BigInteger incrementAndGet() { return bigInteger.accumulateAndGet(BigInteger.ONE, (previous, x) -> previous.add(x)); } public BigInteger getAndIncrement() { return bigInteger.getAndAccumulate(BigInteger.ONE, (previous, x) -> previous.add(x)); } public BigInteger get() { return bigInteger.get(); } }
private void processLiveQueue() { do { ResolvedEvent event; while ((event = liveQueue.poll()) != null) { // drop subscription artificial ResolvedEvent if (event.equals(DROP_SUBSCRIPTION_EVENT)) { DropData previousDropData = dropData.getAndAccumulate(UNKNOWN_DROP_DATA, (current, update) -> (current == null) ? update : current); if (previousDropData == null) { previousDropData = UNKNOWN_DROP_DATA; } dropSubscription(previousDropData.reason, previousDropData.exception); isProcessing.compareAndSet(true, false); return; } try { tryProcess(event); } catch (Exception e) { dropSubscription(SubscriptionDropReason.EventHandlerException, e); return; } } isProcessing.compareAndSet(true, false); } while (!liveQueue.isEmpty() && isProcessing.compareAndSet(false, true)); }
private void processLiveQueue() { do { ResolvedEvent event; while ((event = liveQueue.poll()) != null) { // drop subscription artificial ResolvedEvent if (event.equals(DROP_SUBSCRIPTION_EVENT)) { DropData previousDropData = dropData.getAndAccumulate(UNKNOWN_DROP_DATA, (current, update) -> (current == null) ? update : current); if (previousDropData == null) { previousDropData = UNKNOWN_DROP_DATA; } dropSubscription(previousDropData.reason, previousDropData.exception); isProcessing.compareAndSet(true, false); return; } try { tryProcess(event); } catch (Exception e) { dropSubscription(SubscriptionDropReason.EventHandlerException, e); return; } } isProcessing.compareAndSet(true, false); } while (!liveQueue.isEmpty() && isProcessing.compareAndSet(false, true)); }
DropData previousDropData = dropData.getAndAccumulate(UNKNOWN_DROP_DATA, (current, update) -> (current == null) ? update : current);
DropData previousDropData = dropData.getAndAccumulate(UNKNOWN_DROP_DATA, (current, update) -> (current == null) ? update : current);