/** 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()); done.await(); } }; 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()); }
static void test(ForkJoinPool pool, int num) throws Exception { int ps = pool.getParallelism(); long start = System.nanoTime(); DynamicFib f = new DynamicFib(num); pool.invoke(f); long time = System.nanoTime() - start; double secs = ((double) time) / NPS; long result = f.number; System.out.print("DynamicFib " + num + " = " + result); System.out.printf("\tTime: %9.3f", secs); long sc = pool.getStealCount(); long ns = sc - lastStealCount; lastStealCount = sc; System.out.printf(" Steals: %4d", ns / ps); System.out.printf(" Workers: %4d", pool.getPoolSize()); System.out.println(); }
// 16、实现showLog()方法,接受一个ForkJoinPool对象作为参数,输出关于它的状态信息及线程、任务的执行信息 private static void showLog(ForkJoinPool pool) { System.out.printf("**********************\n"); System.out.printf("Main: Fork/Join Pool log\n"); System.out.printf("Main: Fork/Join Pool: Parallelism: %d\n", pool.getParallelism()); System.out.printf("Main: Fork/Join Pool: Pool Size: %d\n", pool.getPoolSize()); System.out.printf( "Main: Fork/Join Pool: Active Thread Count: %d\n", pool.getActiveThreadCount()); System.out.printf( "Main: Fork/Join Pool: Running Thread Count: %d\n", pool.getRunningThreadCount()); System.out.printf( "Main: Fork/Join Pool: Queued Submission: %d\n", pool.getQueuedSubmissionCount()); System.out.printf("Main: Fork/Join Pool: Queued Tasks: %d\n", pool.getQueuedTaskCount()); System.out.printf( "Main: Fork/Join Pool: Queued Submissions: %s\n", pool.hasQueuedSubmissions()); System.out.printf("Main: Fork/Join Pool: Steal Count: %d\n", pool.getStealCount()); System.out.printf("Main: Fork/Join Pool: Terminated : %s\n", pool.isTerminated()); System.out.printf("**********************\n"); }