本文整理汇总了Java中java.util.concurrent.ForkJoinPool.commonPool方法的典型用法代码示例。如果您正苦于以下问题:Java ForkJoinPool.commonPool方法的具体用法?Java ForkJoinPool.commonPool怎么用?Java ForkJoinPool.commonPool使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ForkJoinPool
的用法示例。
在下文中一共展示了ForkJoinPool.commonPool方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testCreateKeyMultithreaded
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
@Test
public void testCreateKeyMultithreaded() {
final int count = 100000;
final Collection<Callable<String>> tasks = IntStream.range(0, count).boxed()
.map(i -> (Callable<String>) () -> KeyGenerator.createKey()).collect(Collectors.toList());
final ForkJoinPool pool = ForkJoinPool.commonPool();
final List<Future<String>> results = pool.invokeAll(tasks);
final Set<String> keys = results.stream().map(t -> {
try {
return t.get();
} catch (InterruptedException | ExecutionException e) {
throw new IllegalStateException(e);
}
}).collect(Collectors.toSet());
Assert.assertEquals("If " + count + " key generations are performed in parallel, it should yield " + count
+ " of distinct keys", count, keys.size());
}
示例2: dispatch
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
public static void dispatch(int trafficUnitsNumber, double timeSec, DateLocation dateLocation, double[] speedLimitByLane) {
ExecutorService execService = ForkJoinPool.commonPool();
try (SubmissionPublisher<Integer> publisher = new SubmissionPublisher<>()){
subscribe(publisher, execService, Process.AVERAGE_SPEED, timeSec, dateLocation, speedLimitByLane);
subscribe(publisher, execService, Process.TRAFFIC_DENSITY, timeSec, dateLocation, speedLimitByLane);
publisher.submit(trafficUnitsNumber);
} finally {
try {
execService.shutdown();
execService.awaitTermination(1, TimeUnit.SECONDS);
} catch (Exception ex) {
System.out.println("Caught around execService.awaitTermination(): " + ex.getClass().getName());
} finally {
execService.shutdownNow();
}
}
}
示例3: demo4_Flow_submissionPublisher
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
private static void demo4_Flow_submissionPublisher() {
System.out.println();
ExecutorService execService = ForkJoinPool.commonPool();//Executors.newFixedThreadPool(3);
try (SubmissionPublisher<Integer> publisher = new SubmissionPublisher<>()){//execService, 1)){
demoSubscribe(publisher, execService, "One");
demoSubscribe(publisher, execService, "Two");
demoSubscribe(publisher, execService, "Three");
IntStream.range(1, 5).forEach(publisher::submit);
} finally {
try {
execService.shutdown();
int shutdownDelaySec = 1;
System.out.println("Waiting for " + shutdownDelaySec + " sec before shutting down service...");
execService.awaitTermination(shutdownDelaySec, TimeUnit.SECONDS);
} catch (Exception ex) {
System.out.println("Caught around execService.awaitTermination(): " + ex.getClass().getName());
} finally {
System.out.println("Calling execService.shutdownNow()...");
List<Runnable> l = execService.shutdownNow();
System.out.println(l.size() + " tasks were waiting to be executed. Service stopped.");
}
}
}
示例4: Flow_customsubmissionPublisher
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
private static void Flow_customsubmissionPublisher() {
ExecutorService execService = ForkJoinPool.commonPool();//Executors.newFixedThreadPool(3);
try (DockerXDemoPublisher<Integer> publisher = new DockerXDemoPublisher<>(execService)){
demoSubscribe(publisher, "One");
demoSubscribe(publisher, "Two");
demoSubscribe(publisher, "Three");
IntStream.range(1, 5).forEach(publisher::submit);
} finally {
try {
execService.shutdown();
int shutdownDelaySec = 1;
System.out.println("………………等待 " + shutdownDelaySec + " 秒后结束服务……………… ");
execService.awaitTermination(shutdownDelaySec, TimeUnit.SECONDS);
} catch (Exception ex) {
System.out.println("捕获到 execService.awaitTermination()方法的异常: " + ex.getClass().getName());
} finally {
System.out.println("调用 execService.shutdownNow()结束服务...");
List<Runnable> l = execService.shutdownNow();
System.out.println("还剩 "+l.size() + " 个任务等待被执行,服务已关闭 ");
}
}
}
示例5: onCompletedImpl
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
private void onCompletedImpl(@NotNull Runnable continuation, boolean useExecutionContext) {
Executor executor;
if (continueOnCapturedContext) {
SynchronizationContext synchronizationContext = SynchronizationContext.getCurrent();
if (synchronizationContext != null && synchronizationContext.getClass() != SynchronizationContext.class) {
executor = synchronizationContext;
} else {
executor = ForkJoinPool.commonPool();
}
} else {
executor = ForkJoinPool.commonPool();
}
Runnable wrappedContinuation = useExecutionContext ? ExecutionContext.wrap(continuation) : continuation;
future.whenCompleteAsync((result, exception) -> wrappedContinuation.run(), executor);
}
示例6: demo2_ForkJoin_execute_join
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
private static void demo2_ForkJoin_execute_join() {
System.out.println();
AverageSpeed averageSpeed = createTask();
ForkJoinPool commonPool = ForkJoinPool.commonPool();
commonPool.execute(averageSpeed);
double result = averageSpeed.join();
System.out.println("result = " + result);
}
示例7: init
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
@Setup
public void init() throws Exception {
repoDir = Files.createTempDirectory("jmh-gitrepository.").toFile();
repo = new GitRepository(mock(Project.class), repoDir, format, ForkJoinPool.commonPool(),
System.currentTimeMillis(), AUTHOR);
currentRevision = 1;
for (int i = 0; i < previousCommits; i++) {
addCommit();
}
}
示例8: screenExecutor
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
/**
* Null-checks user executor argument, and translates uses of
* commonPool to asyncPool in case parallelism disabled.
*/
static Executor screenExecutor(Executor e) {
if (!useCommonPool && e == ForkJoinPool.commonPool())
return asyncPool;
if (e == null) throw new NullPointerException();
return e;
}
示例9: testConstructor1
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
/**
* A default-constructed SubmissionPublisher has no subscribers,
* is not closed, has default buffer size, and uses the
* defaultExecutor
*/
public void testConstructor1() {
SubmissionPublisher<Integer> p = new SubmissionPublisher<>();
checkInitialState(p);
assertEquals(p.getMaxBufferCapacity(), Flow.defaultBufferSize());
Executor e = p.getExecutor(), c = ForkJoinPool.commonPool();
if (ForkJoinPool.getCommonPoolParallelism() > 1)
assertSame(e, c);
else
assertNotSame(e, c);
}
示例10: testDefaultExecutor
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
/**
* defaultExecutor by default returns the commonPool if
* it supports more than one thread.
*/
public void testDefaultExecutor() {
CompletableFuture<Integer> f = new CompletableFuture<>();
Executor e = f.defaultExecutor();
Executor c = ForkJoinPool.commonPool();
if (ForkJoinPool.getCommonPoolParallelism() > 1)
assertSame(e, c);
else
assertNotSame(e, c);
}
示例11: LocalComputationManager
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
public LocalComputationManager(LocalComputationConfig config) throws IOException {
this(config, ForkJoinPool.commonPool());
}
示例12: beforeClass
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
@BeforeClass
public static void beforeClass() throws Exception {
pm = new DefaultProjectManager(rootDir.getRoot(), ForkJoinPool.commonPool(), null);
}
示例13: newRepositoryManager
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
private GitRepositoryManager newRepositoryManager() {
return new GitRepositoryManager(mock(Project.class), rootDir(), ForkJoinPool.commonPool());
}
示例14: init
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
@Before
public void init() throws IOException {
m = new RepositoryManagerWrapper(new GitRepositoryManager(mock(Project.class), rootDir.getRoot(),
ForkJoinPool.commonPool()),
RepositoryWrapper::new);
}
示例15: init
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
@BeforeClass
public static void init() throws Exception {
pm = new DefaultProjectManager(rootDir.getRoot(), ForkJoinPool.commonPool(), null);
}