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

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

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

AtomicInteger.getAndIncrement介绍

[英]Atomically increments by one the current value.
[中]以原子方式将当前值增加1。

代码示例

代码示例来源:origin: google/guava

@Override
protected Object handleInvocation(Object p, Method calledMethod, Object[] args)
  throws Throwable {
 assertEquals(method, calledMethod);
 assertEquals(method + " invoked more than once.", 0, called.get());
 for (int i = 0; i < passedArgs.length; i++) {
  assertEquals(
    "Parameter #" + i + " of " + method + " not forwarded", passedArgs[i], args[i]);
 }
 called.getAndIncrement();
 return returnValue;
}

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

private AtomicInteger counter = new AtomicInteger();

public int getNextUniqueIndex() {
 return counter.getAndIncrement();
}

代码示例来源:origin: speedment/speedment

@Override
public Metrics metrics(Pipeline initialPipeline, DbmsType dbmsType) {
  requireNonNull(initialPipeline);
  requireNonNull(dbmsType);
  final AtomicInteger filterCounter = new AtomicInteger();
  traverse(initialPipeline, $ -> filterCounter.getAndIncrement());
  return Metrics.of(filterCounter.get(), filterCounter.get(), 0, 0, 0);
}

代码示例来源:origin: deeplearning4j/dl4j-examples

protected void saveDatasets(DataSetIterator iterator, String dir) {
  AtomicInteger counter = new AtomicInteger(0);
  new File(dir).mkdirs();
  while (iterator.hasNext()) {
    String path = FilenameUtils.concat(dir, "dataset-" + (counter.getAndIncrement()) + ".bin");
    iterator.next().save(new File(path));
    if (counter.get() % 500 == 0)
      log.info("{} datasets saved so far...", counter.get());
  }
}

代码示例来源:origin: apache/kafka

private int nextValue(String topic) {
  AtomicInteger counter = topicCounterMap.get(topic);
  if (null == counter) {
    counter = new AtomicInteger(ThreadLocalRandom.current().nextInt());
    AtomicInteger currentCounter = topicCounterMap.putIfAbsent(topic, counter);
    if (currentCounter != null) {
      counter = currentCounter;
    }
  }
  return counter.getAndIncrement();
}

代码示例来源:origin: apache/incubator-dubbo

private boolean isSkip() {
  int skip = connectSkip.get(); // Growth of skipping times
  if (skip >= 10) { // If the number of skipping times increases by more than 10, take the random number
    if (connectRandom == 0) {
      connectRandom = ThreadLocalRandom.current().nextInt(10);
    }
    skip = 10 + connectRandom;
  }
  if (connectSkipped.getAndIncrement() < skip) { // Check the number of skipping times
    return true;
  }
  connectSkip.incrementAndGet();
  connectSkipped.set(0);
  connectRandom = 0;
  return false;
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void noCancelPreviousRetry() {
  final AtomicInteger counter = new AtomicInteger();
  final AtomicInteger times = new AtomicInteger();
  Observable<Integer> source = Observable.defer(new Callable<ObservableSource<Integer>>() {
    @Override
    public ObservableSource<Integer> call() throws Exception {
      if (times.getAndIncrement() < 4) {
        return Observable.error(new TestException());
      }
      return Observable.just(1);
    }
  })
  .doOnDispose(new Action() {
    @Override
    public void run() throws Exception {
      counter.getAndIncrement();
    }
  });
  source.retry(5)
  .test()
  .assertResult(1);
  assertEquals(0, counter.get());
}

代码示例来源:origin: apache/incubator-shardingsphere

@Override
  public String getDataSource(final String name, final String masterDataSourceName, final List<String> slaveDataSourceNames) {
    AtomicInteger count = COUNT_MAP.containsKey(name) ? COUNT_MAP.get(name) : new AtomicInteger(0);
    COUNT_MAP.putIfAbsent(name, count);
    count.compareAndSet(slaveDataSourceNames.size(), 0);
    return slaveDataSourceNames.get(Math.abs(count.getAndIncrement()) % slaveDataSourceNames.size());
  }
}

代码示例来源:origin: apache/incubator-dubbo

private boolean isSkip() {
  int skip = connectSkip.get(); // Growth of skipping times
  if (skip >= 10) { // If the number of skipping times increases by more than 10, take the random number
    if (connectRandom == 0) {
      connectRandom = ThreadLocalRandom.current().nextInt(10);
    }
    skip = 10 + connectRandom;
  }
  if (connectSkipped.getAndIncrement() < skip) { // Check the number of skipping times
    return true;
  }
  connectSkip.incrementAndGet();
  connectSkipped.set(0);
  connectRandom = 0;
  return false;
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void noCancelPreviousRetry() {
  final AtomicInteger counter = new AtomicInteger();
  final AtomicInteger times = new AtomicInteger();
  Flowable<Integer> source = Flowable.defer(new Callable<Flowable<Integer>>() {
    @Override
    public Flowable<Integer> call() throws Exception {
      if (times.getAndIncrement() < 4) {
        return Flowable.error(new TestException());
      }
      return Flowable.just(1);
    }
  })
  .doOnCancel(new Action() {
    @Override
    public void run() throws Exception {
      counter.getAndIncrement();
    }
  });
  source.retry(5)
  .test()
  .assertResult(1);
  assertEquals(0, counter.get());
}

代码示例来源:origin: jphp-group/jphp

public int nextStatementIndex(Class<?> clazz){
  AtomicInteger atomic = statementIndexes.get(clazz);
  if (atomic == null)
    statementIndexes.put(clazz, atomic = new AtomicInteger());
  return atomic.getAndIncrement();
}

代码示例来源:origin: MovingBlocks/Terasology

private short getNextId() {
  if (nextId.get() > MAX_ID) {
    return UNKNOWN_ID;
  }
  return (short) nextId.getAndIncrement();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void noCancelPreviousRetryWhile() {
  final AtomicInteger counter = new AtomicInteger();
  final AtomicInteger times = new AtomicInteger();
  Observable<Integer> source = Observable.defer(new Callable<ObservableSource<Integer>>() {
    @Override
    public ObservableSource<Integer> call() throws Exception {
      if (times.getAndIncrement() < 4) {
        return Observable.error(new TestException());
      }
      return Observable.just(1);
    }
  })
  .doOnDispose(new Action() {
    @Override
    public void run() throws Exception {
      counter.getAndIncrement();
    }
  });
  source.retry(5, Functions.alwaysTrue())
  .test()
  .assertResult(1);
  assertEquals(0, counter.get());
}

代码示例来源:origin: debezium/debezium

protected Field[] fieldsForColumns(Schema schema, List<Column> columns) {
  Field[] fields = new Field[columns.size()];
  AtomicInteger i = new AtomicInteger(0);
  columns.forEach(column -> {
    Field field = schema.field(column.name()); // may be null if the field is unused ...
    fields[i.getAndIncrement()] = field;
  });
  return fields;
}

代码示例来源:origin: twitter/distributedlog

synchronized void forceSetAllowPermits(boolean allowPermits) {
  epoch.getAndIncrement();
  this.enablePermits = allowPermits;
  LOG.info("EnablePermits = {}, Epoch = {}.", this.enablePermits, epoch.get());
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void noCancelPreviousRetryWhile() {
  final AtomicInteger counter = new AtomicInteger();
  final AtomicInteger times = new AtomicInteger();
  Flowable<Integer> source = Flowable.defer(new Callable<Flowable<Integer>>() {
    @Override
    public Flowable<Integer> call() throws Exception {
      if (times.getAndIncrement() < 4) {
        return Flowable.error(new TestException());
      }
      return Flowable.just(1);
    }
  })
  .doOnCancel(new Action() {
    @Override
    public void run() throws Exception {
      counter.getAndIncrement();
    }
  });
  source.retry(5, Functions.alwaysTrue())
  .test()
  .assertResult(1);
  assertEquals(0, counter.get());
}

代码示例来源:origin: prestodb/presto

private void createPageProducers(int numMergeSources)
{
  AtomicInteger counter = new AtomicInteger(0);
  splitPages = pages.stream()
      .collect(Collectors.groupingBy(it -> counter.getAndIncrement() % numMergeSources))
      .values().stream()
      .collect(toImmutableList());
}

代码示例来源:origin: voldemort/voldemort

private void updateStats(long checkoutTimeNs) {
  long wait = waitNs.getAndAdd(checkoutTimeNs);
  int count = checkouts.getAndIncrement();
  // reset reporting interval if we have used up the current interval
  int interval = this.monitoringInterval.get();
  if(count % interval == interval - 1) {
    // harmless race condition:
    waitNs.set(0);
    checkouts.set(0);
    avgWaitNs.set(wait / count);
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void noCancelPreviousRetryWhile2() {
  final AtomicInteger counter = new AtomicInteger();
  final AtomicInteger times = new AtomicInteger();
  Observable<Integer> source = Observable.defer(new Callable<ObservableSource<Integer>>() {
    @Override
    public ObservableSource<Integer> call() throws Exception {
      if (times.getAndIncrement() < 4) {
        return Observable.error(new TestException());
      }
      return Observable.just(1);
    }
  })
  .doOnDispose(new Action() {
    @Override
    public void run() throws Exception {
      counter.getAndIncrement();
    }
  });
  source.retry(new BiPredicate<Integer, Throwable>() {
    @Override
    public boolean test(Integer a, Throwable b) throws Exception {
      return a < 5;
    }
  })
  .test()
  .assertResult(1);
  assertEquals(0, counter.get());
}

代码示例来源:origin: apache/hbase

@Override
 public long getBackoffTime(ServerName serverName, byte[] region, ServerStatistics stats) {
  AtomicInteger inc = count.get(serverName);
  if (inc == null) {
   inc = new AtomicInteger(0);
   count.put(serverName, inc);
  }
  return inc.getAndIncrement();
 }
}

相关文章