本文整理汇总了Java中java.util.concurrent.ForkJoinPool.submit方法的典型用法代码示例。如果您正苦于以下问题:Java ForkJoinPool.submit方法的具体用法?Java ForkJoinPool.submit怎么用?Java ForkJoinPool.submit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ForkJoinPool
的用法示例。
在下文中一共展示了ForkJoinPool.submit方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
public static void main(String[] args) throws InterruptedException {
long startTime = System.currentTimeMillis();
int count = 0;
for (int i = 1; i < 10; i++) {
count = count + i;
Thread.sleep(1000);
}
System.out.println(count);
long endTime = System.currentTimeMillis(); // 获取结束时间
System.out.println("程序运行时间: " + (startTime - endTime) + "ms");
long startTime1 = System.currentTimeMillis();
CountTask countTask = new CountTask(1, 10);
ForkJoinPool forkJoinPool = new ForkJoinPool();
Future<Integer> futureTask = forkJoinPool.submit(countTask);
try {
System.out.println(futureTask.get());
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long endTime1 = System.currentTimeMillis(); // 获取结束时间
System.out.println("程序运行时间: " + (startTime1 - endTime1) + "ms");
}
示例2: testGetPoolSize
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
/**
* getPoolSize returns number of started workers.
*/
public void testGetPoolSize() {
final CountDownLatch taskStarted = new CountDownLatch(1);
final CountDownLatch done = new CountDownLatch(1);
final ForkJoinPool p = new ForkJoinPool(1);
try (PoolCleaner cleaner = cleaner(p)) {
assertEquals(0, p.getActiveThreadCount());
final Runnable task = new CheckedRunnable() {
public void realRun() throws InterruptedException {
taskStarted.countDown();
assertEquals(1, p.getPoolSize());
assertEquals(1, p.getActiveThreadCount());
await(done);
}};
Future<?> future = p.submit(task);
await(taskStarted);
assertEquals(1, p.getPoolSize());
assertEquals(1, p.getActiveThreadCount());
done.countDown();
}
assertEquals(0, p.getPoolSize());
assertEquals(0, p.getActiveThreadCount());
}
示例3: testSubmitForkJoinTask
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
/**
* Completed submit(ForkJoinTask) returns result
*/
public void testSubmitForkJoinTask() throws Throwable {
ForkJoinPool p = new ForkJoinPool(1);
try (PoolCleaner cleaner = cleaner(p)) {
ForkJoinTask<Integer> f = p.submit(new FibTask(8));
assertEquals(21, (int) f.get());
}
}
示例4: testSubmitAfterShutdown
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
/**
* A task submitted after shutdown is rejected
*/
public void testSubmitAfterShutdown() {
ForkJoinPool p = new ForkJoinPool(1);
try (PoolCleaner cleaner = cleaner(p)) {
p.shutdown();
assertTrue(p.isShutdown());
try {
ForkJoinTask<Integer> f = p.submit(new FibTask(8));
shouldThrow();
} catch (RejectedExecutionException success) {}
}
}
示例5: pool
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
static void pool() throws Exception {
System.out.println("pool()");
ForkJoinPool fjPool = new ForkJoinPool(2);
fjPool.submit(() -> {
int result = IntStream.range(0, 5)
.parallel()
.peek(it -> System.out.printf("Thread [%s] peek: %d\n", Thread.currentThread().getName(), it))
.sum();
System.out.println("sum: " + result);
});
fjPool.awaitTermination(1, TimeUnit.SECONDS);
}
示例6: main
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
public static void main(String... args) {
List<Integer> collect = IntStream.rangeClosed(1, 300).boxed().collect(Collectors.toList());
ForkJoinPool pool = new ForkJoinPool(5);
pool.submit(new TestTask(collect, 1));
}
示例7: main
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
ForkJoinPool forkJoinPool = new ForkJoinPool();
Future<Long> result = forkJoinPool.submit(new Sum(1, 10000));
System.out.println("Sum:" + result.get());//Sum:50005000
}