当前位置: 首页>>代码示例>>Java>>正文


Java ForkJoinPool.submit方法代码示例

本文整理汇总了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");

}
 
开发者ID:leon66666,项目名称:JavaCommon,代码行数:26,代码来源:ForkJoinTaskDemo.java

示例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());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:ForkJoinPoolTest.java

示例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());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:11,代码来源:ForkJoinPoolTest.java

示例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) {}
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:15,代码来源:ForkJoinPoolTest.java

示例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);
}
 
开发者ID:vitaly-chibrikov,项目名称:otus_java_2017_10,代码行数:13,代码来源:Parallel.java

示例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));
}
 
开发者ID:daishicheng,项目名称:outcomes,代码行数:6,代码来源:TestTask.java

示例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
}
 
开发者ID:lihengming,项目名称:java-codes,代码行数:6,代码来源:ForkJoinTest.java


注:本文中的java.util.concurrent.ForkJoinPool.submit方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。