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

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

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

AtomicInteger.set介绍

[英]Sets to the given value.
[中]设置为给定的值。

代码示例

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

public boolean isAllowable() {
  long now = System.currentTimeMillis();
  if (now > lastResetTime + interval) {
    token.set(rate);
    lastResetTime = now;
  }
  int value = token.get();
  boolean flag = false;
  while (value > 0 && !flag) {
    flag = token.compareAndSet(value, value - 1);
    value = token.get();
  }
  return flag;
}

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

private int getCurrentId() {
  if (idGenerator.get() > MAX_ID) {
    idGenerator.set(0);
  }
  return idGenerator.incrementAndGet();
}

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

@Test
public void testSynchronousDisconnect() {
  final AtomicInteger effectCounter = new AtomicInteger();
  Observable<Integer> source = Observable.just(1, 2, 3, 4)
  .doOnNext(new Consumer<Integer>() {
    effectCounter.set(0);
    System.out.printf("- %d -%n", i);
    result.subscribe(new Consumer<Integer>() {
    assertEquals(2, effectCounter.get());

代码示例来源:origin: linkedin/parseq

@Test
public void testOnSuccessCalled() {
 final AtomicInteger sequence = new AtomicInteger(0);
 final AtomicInteger action = new AtomicInteger();
 final AtomicInteger onSuccess = new AtomicInteger();
 Continuations CONT = new Continuations();
 CONT.doSubmit(() -> {
  action.set(sequence.incrementAndGet());
 });
 CONT.doSubmit(() -> {
  onSuccess.set(sequence.incrementAndGet());
 });
 assertEquals(action.get(), 1);
 assertEquals(onSuccess.get(), 2);
}

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

@Override
public List<Integer> chooseTasks(int taskId, List<Object> values) {
  int rightNow;
  while (true) {
    rightNow = current.incrementAndGet();
    if (rightNow < capacity) {
      return rets[choices[rightNow]];
    } else if (rightNow == capacity) {
      current.set(0);
      return rets[choices[0]];
    }
    //race condition with another thread, and we lost
    // try again
  }
}

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

@Test
public void testBilto1() {
 context.runOnContext(v1 -> {
  buffer = new InboundBuffer<>(context, 4L);
  AtomicInteger drained = new AtomicInteger();
  AtomicInteger expectedDrained = new AtomicInteger();
  buffer.drainHandler(v2 -> {
   switch (drained.getAndIncrement()) {
    case 0:
     // Check that emitting again will not drain again
     expectedDrained.set(1);
     assertTrue(emit());
     context.runOnContext(v -> {
      assertEquals(1, drained.get());
      testComplete();
     });
     break;
   }
  });
  buffer.handler(val -> {
   if (val == 0) {
    // This will set writable to false
    fill();
   }
   assertEquals(expectedDrained.get(), drained.get());
  });
  assertTrue(emit());
 });
 await();
}

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

/**
 * The {@code FileRecords.open} methods should be used instead of this constructor whenever possible.
 * The constructor is visible for tests.
 */
FileRecords(File file,
      FileChannel channel,
      int start,
      int end,
      boolean isSlice) throws IOException {
  this.file = file;
  this.channel = channel;
  this.start = start;
  this.end = end;
  this.isSlice = isSlice;
  this.size = new AtomicInteger();
  if (isSlice) {
    // don't check the file size if this is just a slice view
    size.set(end - start);
  } else {
    if (channel.size() > Integer.MAX_VALUE)
      throw new KafkaException("The size of segment " + file + " (" + channel.size() +
          ") is larger than the maximum allowed segment size of " + Integer.MAX_VALUE);
    int limit = Math.min((int) channel.size(), end);
    size.set(limit - start);
    // if this is not a slice, update the file pointer to the end of the file
    // set the file position to the last byte in the file
    channel.position(limit);
  }
  batches = batchesFrom(start);
}

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

efforts.getAndIncrement();
active.getAndIncrement();
maxActive.set(Math.max(active.get(), maxActive.get()));
final Thread thread = new Thread(context) {
  @Override

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

@Test(dataProvider = "unsuccessful")
public void getWhenSuccessful_fails(CompletableFuture<?> future) {
 if ((future != null) && !future.isDone()) {
  AtomicInteger result = new AtomicInteger();
  ConcurrentTestHarness.execute(() -> {
   result.set(1);
   Object value = Async.getWhenSuccessful(future);
   result.set((value == null) ? 2 : 3);
  });
  Awaits.await().untilAtomic(result, is(1));
  future.obtrudeException(new IllegalStateException());
  Awaits.await().untilAtomic(result, is(not(1)));
  assertThat(result.get(), is(2));
 }
 assertThat(Async.getWhenSuccessful(future), is(nullValue()));
}

代码示例来源:origin: linkedin/parseq

@Test
public void testOnSuccessNotCalledWhenException() {
 final AtomicInteger sequence = new AtomicInteger(0);
 final AtomicInteger onSuccess = new AtomicInteger();
 Continuations CONT = new Continuations();
 try {
  CONT.doSubmit(() -> {
   throw new RuntimeException("test");
  });
  CONT.doSubmit(() -> {
   onSuccess.set(sequence.incrementAndGet());
  });
  fail("should have thrown exception");
 } catch (Exception e) {
  assertEquals(e.getMessage(), "test");
 }
 assertEquals(onSuccess.get(), 0);
}

代码示例来源:origin: alibaba/jstorm

/**
 * select 1/freq
 */
public boolean countCheck() {
  i.incrementAndGet();
  if (i.get() > freq) {
    target = r.nextInt(freq);
    i.set(0);
  }
  return i.get() == target;
}

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

public boolean isAllowable() {
  long now = System.currentTimeMillis();
  if (now > lastResetTime + interval) {
    token.set(rate);
    lastResetTime = now;
  }
  int value = token.get();
  boolean flag = false;
  while (value > 0 && !flag) {
    flag = token.compareAndSet(value, value - 1);
    value = token.get();
  }
  return flag;
}

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

@Override
  public List<Integer> chooseTasks(int taskId, List<Object> values) {
    int rightNow;
    int size = choices.size();
    while (true) {
      rightNow = current.incrementAndGet();
      if (rightNow < size) {
        return choices.get(rightNow);
      } else if (rightNow == size) {
        current.set(0);
        return choices.get(0);
      }
    } // race condition with another thread, and we lost. try again
  }
}

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

@Test
public void filters() {
  AtomicInteger filterCount = new AtomicInteger();
        assertEquals(4, filterCount.get());
      })
      .verifyComplete();
  filterCount.set(0);

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

@Test
public void getWhenSuccessful_success_async() {
 CompletableFuture<Integer> future = new CompletableFuture<Integer>();
 AtomicInteger result = new AtomicInteger();
 ConcurrentTestHarness.execute(() -> {
  result.set(1);
  result.set(Async.getWhenSuccessful(future));
 });
 Awaits.await().untilAtomic(result, is(1));
 future.obtrudeValue(2);
 Awaits.await().untilAtomic(result, is(2));
}

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

efforts.getAndIncrement();
active.getAndIncrement();
maxActive.set(Math.max(active.get(), maxActive.get()));
final Thread thread = new Thread(context) {
  @Override

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

@Test
public void testSubscribe(){
  List<Integer> expected = Arrays.asList( 10, 9, 9,  8, 7, 6,  6, 5, 5, 5, 4, 3, 3, 2, 2, 1, 1, 0);
  final AtomicInteger i = new AtomicInteger();
  Observable.from(expected).doOnNext(x -> {
    logger.info("print " + x);
    i.set(x);
  }).doOnError(e -> logger.error(e.getMessage())).subscribe();
  logger.info("last");
  assertTrue(i.get()==0);
}

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

@Test
 public void testConsistentCloseOrder()
 {
  final AtomicInteger closed1 = new AtomicInteger();
  final AtomicInteger closed2 = new AtomicInteger();
  final AtomicInteger counter = new AtomicInteger();

  Sequence<Integer> sequence = Sequences
    .simple(Arrays.asList(1, 2, 3))
    .withBaggage(() -> closed1.set(counter.incrementAndGet()))
    .withBaggage(() -> closed2.set(counter.incrementAndGet()));
  // Run sequence via accumulate
  sequence.toList();
  Assert.assertEquals(1, closed1.get());
  Assert.assertEquals(2, closed2.get());

  // Ensure sequence runs via Yielder, because LimitedSequence extends YieldingSequenceBase
  Sequence<Integer> yieldingSequence = sequence.limit(1);
  yieldingSequence.toList();
  Assert.assertEquals(3, closed1.get());
  Assert.assertEquals(4, closed2.get());
 }
}

相关文章