currentReads.decrement(); } catch (NeedsReadException e) { socket.pendingOps.accumulateAndGet(SelectionKey.OP_READ, new IntBinaryOperator() { @Override public int applyAsInt(final int a, final int b) { selector.wakeup(); } catch (NeedsWriteException e) { socket.pendingOps.accumulateAndGet(SelectionKey.OP_WRITE, new IntBinaryOperator() { @Override public int applyAsInt(final int a, final int b) {
currentWrites.decrement(); } catch (NeedsReadException e) { socket.pendingOps.accumulateAndGet(SelectionKey.OP_READ, new IntBinaryOperator() { @Override public int applyAsInt(final int a, final int b) { selector.wakeup(); } catch (NeedsWriteException e) { socket.pendingOps.accumulateAndGet(SelectionKey.OP_WRITE, new IntBinaryOperator() { @Override public int applyAsInt(final int a, final int b) {
private static void testAccumulate() { atomicInt.set(0); ExecutorService executor = Executors.newFixedThreadPool(2); IntStream.range(0, NUM_INCREMENTS) .forEach(i -> { Runnable task = () -> atomicInt.accumulateAndGet(i, (n, m) -> n + m); executor.submit(task); }); ConcurrentUtils.stop(executor); System.out.format("Accumulate: %d\n", atomicInt.get()); }
public void addMissingRequests(){ int missed=1; long toRequest=0L; do { long localQueued = queued.getAndSet(0l); Subscription localSub = next.getAndSet(null); long missedOutput = produced.get(); Subscription localActive = active.get(); long reqs = requested.get() + localQueued; if(reqs<0 || toRequest<0) { processAll=true; if(localSub!=null) localSub.request(Long.MAX_VALUE); if(localActive!=null) localActive.request(Long.MAX_VALUE); return; } requested.set(reqs); if(localSub!=null){ active.set(localSub); toRequest +=reqs; }else if(localQueued !=0 && localActive!=null) { toRequest += reqs; } missed = wip.accumulateAndGet(missed,(a,b)->a-b); }while(missed!=0); if(toRequest>0) active.get().request(toRequest); } public void emitted(long n) {
toRequest += reqs; missed = wip.accumulateAndGet(missed,(a,b)->a-b);
void trackReadRequest() { final int curr = readsInProgress.incrementAndGet(); maxReadsInProgress.accumulateAndGet(curr, Integer::max); }
@Override public void write(char[] cbuf, int off, int len) throws IOException { int lock = lockId.accumulateAndGet(1, (x1, x2) -> { if (x1 + x2 >= numberOfFiles) { return 0; } return x1 + x2; }); this.writers[lock].write(cbuf, off, len); }
public class CyclicCounter { private final int maxVal; private final AtomicInteger counter = new AtomicInteger(0); public CyclicCounter(int maxVal) { this.maxVal = maxVal; } public int cyclicallyIncrementAndGet() { return counter.accumulateAndGet(1, new IntBinaryOperator(){ @Override public int applyAsInt(int index, int increment) { return ++index >= maxVal ? 0 : index; }}); } }
void trackAddRequest() { final int curr = addsInProgress.incrementAndGet(); maxAddsInProgress.accumulateAndGet(curr, Integer::max); }
private static void testAccumulate() { atomicInt.set(0); ExecutorService executor = Executors.newFixedThreadPool(2); IntStream.range(0, NUM_INCREMENTS) .forEach(i -> { Runnable task = () -> atomicInt.accumulateAndGet(i, (n, m) -> n + m); executor.submit(task); }); ConcurrentUtils.stop(executor); System.out.format("Accumulate: %d\n", atomicInt.get()); }
void increment(int incValue, AtomicInteger i) { // The lambda is closed over incValue. Because of this the created IntUnaryOperator // will have a field which contains incValue. Because of this a new instance must // be allocated on every call to the increment method. i.updateAndGet(value -> incValue + value); // The lambda is not closed over anything. The same IntBinaryOperator instance // can be used on every call to the increment method, nothing need to be allocated. i.accumulateAndGet(incValue, (incValueParam, value) -> incValueParam + value); }
public void addMissingRequests(){ int missed=1; long toRequest=0L; do { long localQueued = queued.getAndSet(0l); Subscription localSub = next.getAndSet(null); long missedOutput = produced.get(); Subscription localActive = active.get(); long reqs = requested.get() + localQueued; if(reqs<0 || toRequest<0) { processAll=true; if(localSub!=null) localSub.request(Long.MAX_VALUE); if(localActive!=null) localActive.request(Long.MAX_VALUE); return; } requested.set(reqs); if(localSub!=null){ active.set(localSub); toRequest +=reqs; }else if(localQueued !=0 && localActive!=null) { toRequest += reqs; } missed = wip.accumulateAndGet(missed,(a,b)->a-b); }while(missed!=0); if(toRequest>0) active.get().request(toRequest); } public void emitted(long n) {
toRequest += reqs; missed = wip.accumulateAndGet(missed,(a,b)->a-b);
toRequest += reqs; missed = wip.accumulateAndGet(missed,(a,b)->a-b);
public void addMissingRequests(){ int missed=1; long toRequest=0L; do { long localQueued = queued.getAndSet(0l); Subscription localSub = next.getAndSet(null); long missedOutput = produced.get(); Subscription localActive = active.get(); long reqs = requested.get() + localQueued; if(reqs<0 || toRequest<0) { processAll=true; if(localSub!=null) localSub.request(Long.MAX_VALUE); if(localActive!=null) localActive.request(Long.MAX_VALUE); return; } requested.set(reqs); if(localSub!=null){ active.set(localSub); toRequest +=reqs; }else if(localQueued !=0 && localActive!=null) { toRequest += reqs; } missed = wip.accumulateAndGet(missed,(a,b)->a-b); }while(missed!=0); if(toRequest>0) active.get().request(toRequest); } public void emitted(long n) {
private void addThreads( List<WorkerThread> threads, Model model, int count, CompletableFuture<Boolean> future) { WorkerStateListener listener = new WorkerStateListener(future, count); int maxGpu = configManager.getNumberOfGpu(); for (int i = 0; i < count; ++i) { int gpuId = -1; if (maxGpu > 0) { gpuId = gpuCounter.accumulateAndGet(maxGpu, (prev, maxGpuId) -> ++prev % maxGpuId); } BatchAggregator aggregator = new BatchAggregator(model); WorkerThread thread = new WorkerThread( configManager, backendGroup, configManager.isDebug() ? port.get() : port.getAndIncrement(), gpuId, model, aggregator, listener); threads.add(thread); threadPool.submit(thread); } }
currentReads.decrement(); } catch (NeedsReadException e) { socket.pendingOps.accumulateAndGet(SelectionKey.OP_READ, new IntBinaryOperator() { @Override public int applyAsInt(final int a, final int b) { selector.wakeup(); } catch (NeedsWriteException e) { socket.pendingOps.accumulateAndGet(SelectionKey.OP_WRITE, new IntBinaryOperator() { @Override public int applyAsInt(final int a, final int b) {
currentWrites.decrement(); } catch (NeedsReadException e) { socket.pendingOps.accumulateAndGet(SelectionKey.OP_READ, new IntBinaryOperator() { @Override public int applyAsInt(final int a, final int b) { selector.wakeup(); } catch (NeedsWriteException e) { socket.pendingOps.accumulateAndGet(SelectionKey.OP_WRITE, new IntBinaryOperator() { @Override public int applyAsInt(final int a, final int b) {
@Test public void testMultipleApplies() { ArrayList<Integer> results = new ArrayList<>(); AtomicInteger results2 = new AtomicInteger(); OSGi<Integer> program = OSGi.combine( (a, b, c) -> a + b + c, just(Arrays.asList(5, 20)), just(Arrays.asList(5, 40)), just(Arrays.asList(5, 60))); OSGiResult or = program.run(bundleContext, newValue -> { results.add(newValue); return NOOP; }); or.close(); OSGiResult or2 = program.run( bundleContext, i -> { results2.accumulateAndGet(i, (a, b) -> a + b); return NOOP; }); or2.close(); assertEquals(8, results.size()); assertEquals(540, results2.get()); }