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

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

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

AtomicInteger.<init>介绍

[英]Creates a new AtomicInteger with initial value 0.
[中]创建初始值为0的新AtomicInteger。

代码示例

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

private AtomicInteger counter = new AtomicInteger();

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

代码示例来源: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: ReactiveX/RxJava

@Test
public void testUnsubscribeOnNestedTakeAndSyncInfiniteStream() throws InterruptedException {
  final AtomicInteger subscribeCounter = new AtomicInteger();
  final AtomicInteger sentEventCounter = new AtomicInteger();
  doTestUnsubscribeOnNestedTakeAndAsyncInfiniteStream(SYNC_INFINITE_OBSERVABLE_OF_EVENT(2, subscribeCounter, sentEventCounter), subscribeCounter);
  Thread.sleep(500);
  assertEquals(39, sentEventCounter.get());
}

代码示例来源: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: eclipse-vertx/vert.x

@Test
public void testEmitReentrancy() {
 AtomicInteger count = new AtomicInteger(2);
 AtomicBoolean emitting = new AtomicBoolean();
 stream.pause();
 stream.fetch(3);
 stream.handler(item -> {
  assertFalse(emitting.getAndSet(true));
  emitted.add(item);
  stream.emit(count.getAndIncrement());
  emitting.set(false);
 });
 stream.emit(0, 1);
 assertEquals(Arrays.asList(0, 1, 2), emitted);
}

代码示例来源: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: google/guava

public void testForEach() {
 ImmutableIntArray.of().forEach(i -> fail());
 ImmutableIntArray.of(0, 1, 3).subArray(1, 1).forEach(i -> fail());
 AtomicInteger count = new AtomicInteger(0);
 ImmutableIntArray.of(0, 1, 2, 3).forEach(i -> assertThat(i).isEqualTo(count.getAndIncrement()));
 assertEquals(4, count.get());
}

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

/**
 * On a successful concurrent computation, only one thread does the work, but all the threads get
 * the same result.
 */
private static void testConcurrentLoadingDefault(CacheBuilder<Object, Object> builder)
  throws InterruptedException {
 int count = 10;
 final AtomicInteger callCount = new AtomicInteger();
 final CountDownLatch startSignal = new CountDownLatch(count + 1);
 final Object result = new Object();
 LoadingCache<String, Object> cache =
   builder.build(
     new CacheLoader<String, Object>() {
      @Override
      public Object load(String key) throws InterruptedException {
       callCount.incrementAndGet();
       startSignal.await();
       return result;
      }
     });
 List<Object> resultArray = doConcurrentGet(cache, "bar", count, startSignal);
 assertEquals(1, callCount.get());
 for (int i = 0; i < count; i++) {
  assertSame("result(" + i + ") didn't match expected", result, resultArray.get(i));
 }
}

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

@Test
public void testUnsubscribeOnNestedTakeAndAsyncInfiniteStream() throws InterruptedException {
  final AtomicInteger subscribeCounter = new AtomicInteger();
  final AtomicInteger sentEventCounter = new AtomicInteger();
  doTestUnsubscribeOnNestedTakeAndAsyncInfiniteStream(ASYNC_INFINITE_OBSERVABLE_OF_EVENT(2, subscribeCounter, sentEventCounter), subscribeCounter);
  Thread.sleep(500);
  assertEquals(39, sentEventCounter.get());
}

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

public void testForEachPair_parallel() {
 Stream<String> streamA = IntStream.range(0, 100000).mapToObj(String::valueOf).parallel();
 Stream<Integer> streamB = IntStream.range(0, 100000).mapToObj(i -> i).parallel();
 AtomicInteger count = new AtomicInteger(0);
 Streams.forEachPair(
   streamA,
   streamB,
   (a, b) -> {
    count.incrementAndGet();
    Truth.assertThat(a.equals(String.valueOf(b))).isTrue();
   });
 Truth.assertThat(count.get()).isEqualTo(100000);
 // of course, this test doesn't prove that anything actually happened in parallel...
}

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

@Test
public void testFlowing() {
 context.runOnContext(v -> {
  buffer = new InboundBuffer<>(context);
  AtomicInteger events = new AtomicInteger();
  buffer.handler(elt -> {
   checkContext();
   assertEquals(0, (int)elt);
   assertEquals(0, events.getAndIncrement());
   testComplete();
  });
  assertTrue(emit());
 });
 await();
}

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

@Override
 public AtomicInteger load(Integer key) {
  applyCount.incrementAndGet();
  return new AtomicInteger();
 }
};

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

public void testForEach() {
 ImmutableDoubleArray.of().forEach(i -> fail());
 ImmutableDoubleArray.of(0, 1, 3).subArray(1, 1).forEach(i -> fail());
 AtomicInteger count = new AtomicInteger(0);
 ImmutableDoubleArray.of(0, 1, 2, 3)
   .forEach(i -> assertThat(i).isEqualTo((double) count.getAndIncrement()));
 assertEquals(4, count.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: ReactiveX/RxJava

@Test
public void testUnsubscribeOnNestedTakeAndSyncInfiniteStream() throws InterruptedException {
  final AtomicInteger subscribeCounter = new AtomicInteger();
  final AtomicInteger sentEventCounter = new AtomicInteger();
  doTestUnsubscribeOnNestedTakeAndAsyncInfiniteStream(SYNC_INFINITE_OBSERVABLE_OF_EVENT(2, subscribeCounter, sentEventCounter), subscribeCounter);
  Thread.sleep(500);
  assertEquals(39, sentEventCounter.get());
}

代码示例来源:origin: ben-manes/caffeine

@Test(dataProvider = "empty")
public void manyProducers_noConsumer(MpscGrowableArrayQueue<Integer> buffer) {
 AtomicInteger count = new AtomicInteger();
 ConcurrentTestHarness.timeTasks(NUM_PRODUCERS, () -> {
  for (int i = 0; i < PRODUCE; i++) {
   if (buffer.offer(i)) {
    count.incrementAndGet();
   }
  }
 });
 assertThat(buffer.size(), is(count.get()));
}

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

private static void insertIntoReplica(Map<Integer, AtomicInteger> replica, int newValue) {
 if (replica.containsKey(newValue)) {
  replica.get(newValue).incrementAndGet();
 } else {
  replica.put(newValue, new AtomicInteger(1));
 }
}

代码示例来源: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: ReactiveX/RxJava

@Test
public void testUnsubscribeOnNestedTakeAndAsyncInfiniteStream() throws InterruptedException {
  final AtomicInteger subscribeCounter = new AtomicInteger();
  final AtomicInteger sentEventCounter = new AtomicInteger();
  doTestUnsubscribeOnNestedTakeAndAsyncInfiniteStream(ASYNC_INFINITE_OBSERVABLE_OF_EVENT(2, subscribeCounter, sentEventCounter), subscribeCounter);
  Thread.sleep(500);
  assertEquals(39, sentEventCounter.get());
}

代码示例来源: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();
  }
});

相关文章