本文整理汇总了Java中java.util.concurrent.atomic.AtomicInteger.updateAndGet方法的典型用法代码示例。如果您正苦于以下问题:Java AtomicInteger.updateAndGet方法的具体用法?Java AtomicInteger.updateAndGet怎么用?Java AtomicInteger.updateAndGet使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.atomic.AtomicInteger
的用法示例。
在下文中一共展示了AtomicInteger.updateAndGet方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: workerRun
import java.util.concurrent.atomic.AtomicInteger; //导入方法依赖的package包/类
/**
* Worker run
*/
private void workerRun(Set<Long> uidSet, AtomicInteger control) {
for (;;) {
int myPosition = control.updateAndGet(old -> (old == SIZE ? SIZE : old + 1));
if (myPosition == SIZE) {
return;
}
doGenerate(uidSet, myPosition);
}
}
示例2: workerRun
import java.util.concurrent.atomic.AtomicInteger; //导入方法依赖的package包/类
/**
* Woker run
*/
private void workerRun(Set<Long> uidSet, AtomicInteger control) {
for (;;) {
int myPosition = control.updateAndGet(old -> (old == SIZE ? SIZE : old + 1));
if (myPosition == SIZE) {
return;
}
doGenerate(uidSet, myPosition);
}
}
示例3: testConsumerConcurrently
import java.util.concurrent.atomic.AtomicInteger; //导入方法依赖的package包/类
public void testConsumerConcurrently() throws InterruptedException {
int expectedNumResults = randomIntBetween(1, 100);
int bufferSize = randomIntBetween(2, 200);
SearchRequest request = new SearchRequest();
request.source(new SearchSourceBuilder().aggregation(AggregationBuilders.avg("foo")));
request.setBatchedReduceSize(bufferSize);
InitialSearchPhase.SearchPhaseResults<QuerySearchResultProvider> consumer =
searchPhaseController.newSearchPhaseResults(request, expectedNumResults);
AtomicInteger max = new AtomicInteger();
CountDownLatch latch = new CountDownLatch(expectedNumResults);
for (int i = 0; i < expectedNumResults; i++) {
int id = i;
Thread t = new Thread(() -> {
int number = randomIntBetween(1, 1000);
max.updateAndGet(prev -> Math.max(prev, number));
QuerySearchResult result = new QuerySearchResult(id, new SearchShardTarget("node", new Index("a", "b"), id));
result.topDocs(new TopDocs(id, new ScoreDoc[0], 0.0F), new DocValueFormat[0]);
InternalAggregations aggs = new InternalAggregations(Arrays.asList(new InternalMax("test", (double) number,
DocValueFormat.RAW, Collections.emptyList(), Collections.emptyMap())));
result.aggregations(aggs);
consumer.consumeResult(id, result);
latch.countDown();
});
t.start();
}
latch.await();
SearchPhaseController.ReducedQueryPhase reduce = consumer.reduce();
InternalMax internalMax = (InternalMax) reduce.aggregations.asList().get(0);
assertEquals(max.get(), internalMax.getValue(), 0.0D);
}