/** awaitTermination on a non-shutdown pool times out */ public void testAwaitTermination_timesOut() throws InterruptedException { ForkJoinPool p = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(p)) { assertFalse(p.isTerminated()); assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); assertFalse(p.awaitTermination(-1L, NANOSECONDS)); assertFalse(p.awaitTermination(-1L, MILLISECONDS)); assertFalse(p.awaitTermination(0L, NANOSECONDS)); assertFalse(p.awaitTermination(0L, MILLISECONDS)); long timeoutNanos = 999999L; long startTime = System.nanoTime(); assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); assertTrue(System.nanoTime() - startTime >= timeoutNanos); assertFalse(p.isTerminated()); startTime = System.nanoTime(); long timeoutMillis = timeoutMillis(); assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); assertTrue(millisElapsedSince(startTime) >= timeoutMillis); assertFalse(p.isTerminated()); p.shutdown(); assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); assertTrue(p.isTerminated()); } }
/** * After invoking a single task, isQuiescent eventually becomes true, at which time queues are * empty, threads are not active, the task has completed successfully, and construction parameters * continue to hold */ public void testIsQuiescent() throws Exception { ForkJoinPool p = new ForkJoinPool(2); try (PoolCleaner cleaner = cleaner(p)) { assertTrue(p.isQuiescent()); long startTime = System.nanoTime(); FibTask f = new FibTask(20); p.invoke(f); assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, p.getFactory()); while (!p.isQuiescent()) { if (millisElapsedSince(startTime) > LONG_DELAY_MS) throw new AssertionFailedError("timed out"); assertFalse(p.getAsyncMode()); assertFalse(p.isShutdown()); assertFalse(p.isTerminating()); assertFalse(p.isTerminated()); Thread.yield(); } assertTrue(p.isQuiescent()); assertFalse(p.getAsyncMode()); assertEquals(0, p.getQueuedTaskCount()); assertEquals(0, p.getQueuedSubmissionCount()); assertFalse(p.hasQueuedSubmissions()); while (p.getActiveThreadCount() != 0 && millisElapsedSince(startTime) < LONG_DELAY_MS) Thread.yield(); assertFalse(p.isShutdown()); assertFalse(p.isTerminating()); assertFalse(p.isTerminated()); assertTrue(f.isDone()); assertEquals(6765, (int) f.get()); assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } }
/** * Successfully constructed pool reports default factory, parallelism and async mode policies, no * active threads or tasks, and quiescent running state. */ public void testDefaultInitialState() { ForkJoinPool p = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(p)) { assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, p.getFactory()); assertFalse(p.getAsyncMode()); assertEquals(0, p.getActiveThreadCount()); assertEquals(0, p.getStealCount()); assertEquals(0, p.getQueuedTaskCount()); assertEquals(0, p.getQueuedSubmissionCount()); assertFalse(p.hasQueuedSubmissions()); assertFalse(p.isShutdown()); assertFalse(p.isTerminating()); assertFalse(p.isTerminated()); } }
// 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"); }