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

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

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

AtomicInteger.incrementAndGet介绍

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

代码示例

代码示例来源: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: stackoverflow.com

String[] names = {"Sam", "Pamela", "Dave", "Pascal", "Erik"};
AtomicInteger index = new AtomicInteger();
List<String> list = Arrays.stream(names)
             .filter(n -> n.length() <= index.incrementAndGet())
             .collect(Collectors.toList());

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

/**
 * Add the given request to the queue for the connection it was directed to
 */
public void add(NetworkClient.InFlightRequest request) {
  String destination = request.destination;
  Deque<NetworkClient.InFlightRequest> reqs = this.requests.get(destination);
  if (reqs == null) {
    reqs = new ArrayDeque<>();
    this.requests.put(destination, reqs);
  }
  reqs.addFirst(request);
  inFlightRequestCount.incrementAndGet();
}

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

@Test
public void testImmediateSuccess() throws Exception
{
 final AtomicInteger count = new AtomicInteger();
 final String result = RetryUtils.retry(
   () -> {
    count.incrementAndGet();
    return "hey";
   },
   isTransient,
   2
 );
 Assert.assertEquals("result", "hey", result);
 Assert.assertEquals("count", 1, count.get());
}

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

private void _registerAllocationInHostMap(String host, Map<String, AtomicInteger> hostMap) {
  AtomicInteger val = hostMap.get(host);
  if (val == null) {
   val = new AtomicInteger(0);
   hostMap.put(host, val);
  }
  val.incrementAndGet();
 }
}

代码示例来源:origin: reactive-streams/reactive-streams-jvm

@Override
public void subscribe(Subscriber<? super Integer> s) {
  if (s == null) {
    throw new NullPointerException();
  }
  
  int ids = id.incrementAndGet();
  
  RangeSubscription parent = new RangeSubscription(s, ids, start, start + count);
  stacks.put(ids, stacktrace);
  states.put(ids, false);
  s.onSubscribe(parent);
}

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

@Override
  public void run() {
    final AtomicInteger nexts = new AtomicInteger();
    try {
      Observable<String> origin = Observable.unsafeCreate(new FuncWithErrors(NUM_RETRIES));
      TestObserver<String> to = new TestObserver<String>();
      origin.retry()
      .observeOn(Schedulers.computation()).subscribe(to);
      to.awaitTerminalEvent(2500, TimeUnit.MILLISECONDS);
      List<String> onNextEvents = new ArrayList<String>(to.values());
      if (onNextEvents.size() != NUM_RETRIES + 2) {
        for (Throwable t : to.errors()) {
          onNextEvents.add(t.toString());
        }
        for (long err = to.completions(); err != 0; err--) {
          onNextEvents.add("onComplete");
        }
        data.put(j, onNextEvents);
      }
    } catch (Throwable t) {
      timeouts.incrementAndGet();
      System.out.println(j + " | " + cdl.getCount() + " !!! " + nexts.get());
    }
    cdl.countDown();
  }
});

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

LocalVariable createNew( TypeReference type, String name )
{
  if ( localVariables.containsKey( name ) )
  {
    throw new IllegalStateException( String.format( "Local variable %s already in scope", name  ) );
  }
  LocalVariable localVariable = new LocalVariable( type, name, counter.getAndIncrement() );
  localVariables.put( name, localVariable );
  //if 64 bit types we need to give it one more index
  if ( type.simpleName().equals( "double" ) || type.simpleName().equals( "long" ) )
  {
    counter.incrementAndGet();
  }
  return localVariable;
}

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

/** {@inheritDoc} */
  @Override public void sendMessage(ClusterNode node, Message msg, IgniteInClosure<IgniteException> ackClosure)
    throws IgniteSpiException {
    if (msg instanceof GridIoMessage) {
      GridIoMessage ioMsg = (GridIoMessage)msg;
      Class<?> cls = ioMsg.message().getClass();
      AtomicInteger cntr = msgCntMap.get(cls);
      if (cntr == null)
        cntr = F.addIfAbsent(msgCntMap, cls, new AtomicInteger());
      cntr.incrementAndGet();
    }
    super.sendMessage(node, msg, ackClosure);
  }
}

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

@Override
public void release() {
  if (referenceCount.decrementAndGet() < 0) {
    referenceCount.incrementAndGet();
    throw new IllegalStateException("Attempted to decrement the reference count below 0");
  }
  if (referenceCount.get() == 0) {
    buf = null;
  }
}

代码示例来源:origin: spring-projects/spring-framework

private static int count(Iterable<Node> nodes) {
  assertNotNull(nodes);
  AtomicInteger count = new AtomicInteger();
  nodes.forEach(n -> count.incrementAndGet());
  return count.get();
}

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

@Override
  public void run() {
    if (wip.incrementAndGet() == 2) {
      emit();
      if (wip.decrementAndGet() == 0) {
        downstream.onComplete();
      }
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void verifyDirtiesContextBehavior() throws Exception {
  runTestClassAndAssertStats(ClassLevelDirtiesContextWithCleanMethodsAndDefaultModeTestCase.class, 1);
  assertContextCacheStatistics("after class-level @DirtiesContext with clean test method and default class mode",
    0, cacheHits.get(), cacheMisses.incrementAndGet());
}

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

@Override
public void onNext(Integer t) {
  int total = totalReceived.incrementAndGet();
  received.incrementAndGet();
  if (total >= 2000) {
    dispose();
  }
  if (received.get() == 100) {
    batches.incrementAndGet();
    request(100);
    received.set(0);
  }
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testPublishSameHandlerRegisteredTwice() {
 String str = TestUtils.randomUnicodeString(1000);
 AtomicInteger count = new AtomicInteger();
 Handler<Message<String>> handler = (Message<String> msg) -> {
  assertEquals(str, msg.body());
  if (count.incrementAndGet() == 2) {
   testComplete();
  }
 };
 eb.<String>consumer(ADDRESS1).handler(handler);
 eb.<String>consumer(ADDRESS1).handler(handler);
 eb.publish(ADDRESS1, str);
 await();
}

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

@Test
public void testEventualSuccess() throws Exception
{
 final AtomicInteger count = new AtomicInteger();
 final String result = RetryUtils.retry(
   () -> {
    if (count.incrementAndGet() >= 2) {
     return "hey";
    } else {
     throw new IOException("what");
    }
   },
   isTransient,
   3
 );
 Assert.assertEquals("result", "hey", result);
 Assert.assertEquals("count", 2, count.get());
}

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

@Override
protected void incrementFileID(int fileID) {
 AtomicInteger counter = logFileIDReferenceCounts.get(fileID);
 if (counter == null) {
  counter = new AtomicInteger(0);
  logFileIDReferenceCounts.put(fileID, counter);
 }
 counter.incrementAndGet();
}

代码示例来源:origin: graphql-java/graphql-java

private String remapVariable(String varName, Map<String, String> variableRemapping, AtomicInteger variableCount) {
  String mappedName = variableRemapping.get(varName);
  if (mappedName == null) {
    mappedName = "var" + variableCount.incrementAndGet();
    variableRemapping.put(varName, mappedName);
  }
  return mappedName;
}

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

/** {@inheritDoc} */
@Override public void write(Cache.Entry<?, ?> e) {
  writes.incrementAndGet();
  map.put(e.getKey(), e.getValue());
}

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

@Override
  public void run() {
    final AtomicInteger nexts = new AtomicInteger();
    try {
      Flowable<String> origin = Flowable.unsafeCreate(new FuncWithErrors(numRetries));
      TestSubscriber<String> ts = new TestSubscriber<String>();
      origin.retry()
      .observeOn(Schedulers.computation()).subscribe(ts);
      ts.awaitTerminalEvent(2500, TimeUnit.MILLISECONDS);
      List<String> onNextEvents = new ArrayList<String>(ts.values());
      if (onNextEvents.size() != numRetries + 2) {
        for (Throwable t : ts.errors()) {
          onNextEvents.add(t.toString());
        }
        for (long err = ts.completions(); err != 0; err--) {
          onNextEvents.add("onComplete");
        }
        data.put(j, onNextEvents);
      }
    } catch (Throwable t) {
      timeouts.incrementAndGet();
      System.out.println(j + " | " + cdl.getCount() + " !!! " + nexts.get());
    }
    cdl.countDown();
  }
});

相关文章