java.util.concurrent.atomic.AtomicInteger.accumulateAndGet()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(8.4k)|赞(0)|评价(0)|浏览(110)

本文整理了Java中java.util.concurrent.atomic.AtomicInteger.accumulateAndGet()方法的一些代码示例,展示了AtomicInteger.accumulateAndGet()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AtomicInteger.accumulateAndGet()方法的具体详情如下:
包路径:java.util.concurrent.atomic.AtomicInteger
类名称:AtomicInteger
方法名:accumulateAndGet

AtomicInteger.accumulateAndGet介绍

[英]Atomically updates the current value with the results of applying the given function to the current and given values, returning the updated value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads. The function is applied with the current value as its first argument, and the given update as the second argument.
[中]将给定函数应用于当前值和给定值的结果以原子方式更新当前值,并返回更新后的值。该函数应该没有副作用,因为当尝试的更新由于线程之间的争用而失败时,可以重新应用该函数。应用函数时,当前值作为第一个参数,给定的更新作为第二个参数。

代码示例

代码示例来源:origin: org.mongodb/mongo-java-driver

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) {

代码示例来源:origin: org.mongodb/mongo-java-driver

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) {

代码示例来源:origin: biezhi/learn-java8

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());
}

代码示例来源:origin: aol/cyclops

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) {

代码示例来源:origin: aol/cyclops

toRequest += reqs;
missed = wip.accumulateAndGet(missed,(a,b)->a-b);

代码示例来源:origin: org.apache.bookkeeper/bookkeeper-server

void trackReadRequest() {
  final int curr = readsInProgress.incrementAndGet();
  maxReadsInProgress.accumulateAndGet(curr, Integer::max);
}

代码示例来源:origin: com.davidbracewell/mango

@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);
}

代码示例来源:origin: stackoverflow.com

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;
    }});
 }
}

代码示例来源:origin: org.apache.bookkeeper/bookkeeper-server

void trackAddRequest() {
  final int curr = addsInProgress.incrementAndGet();
  maxAddsInProgress.accumulateAndGet(curr, Integer::max);
}

代码示例来源:origin: dunwu/javacore

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());
}

代码示例来源:origin: stackoverflow.com

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);
}

代码示例来源:origin: com.oath.cyclops/cyclops

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) {

代码示例来源:origin: com.aol.simplereact/cyclops-react

toRequest += reqs;
missed = wip.accumulateAndGet(missed,(a,b)->a-b);

代码示例来源:origin: com.oath.cyclops/cyclops

toRequest += reqs;
missed = wip.accumulateAndGet(missed,(a,b)->a-b);

代码示例来源:origin: com.aol.simplereact/cyclops-react

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) {

代码示例来源:origin: awslabs/mxnet-model-server

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);
  }
}

代码示例来源:origin: org.mongodb/mongodb-driver-core

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) {

代码示例来源:origin: org.mongodb/mongodb-driver-core

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) {

代码示例来源:origin: org.apache.aries.component-dsl/org.apache.aries.component-dsl.itests

@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());
}

相关文章