/** * 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); } }
/** 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()); }
/** * 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()); } }
/** * Main method of the example * * @param args */ public static void main(String[] args) { // Create a list of products ProductListGenerator generator = new ProductListGenerator(); List<Product> products = generator.generate(10000); // Craete a task Task task = new Task(products, 0, products.size(), 0.20); // Create a ForkJoinPool ForkJoinPool pool = new ForkJoinPool(); // Execute the Task pool.execute(task); // Write information about the pool do { System.out.printf("Main: Thread Count: %d\n", pool.getActiveThreadCount()); System.out.printf("Main: Thread Steal: %d\n", pool.getStealCount()); System.out.printf("Main: Paralelism: %d\n", pool.getParallelism()); try { TimeUnit.MILLISECONDS.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } } while (!task.isDone()); // Shutdown the pool pool.shutdown(); // Check if the task has completed normally if (task.isCompletedNormally()) { System.out.printf("Main: The process has completed normally.\n"); } // Expected result: 12. Write products which price is not 12 for (int i = 0; i < products.size(); i++) { Product product = products.get(i); if (product.getPrice() != 12) { System.out.printf("Product %s: %f\n", product.getName(), product.getPrice()); } } // End of the program System.out.println("Main: End of the program.\n"); }
// 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"); }
public static int[] sort(int[] input) { ForkJoinPool mainPool = new ForkJoinPool(); int[] output = new int[input.length]; // from round 0...n, segment length is 2^n. int round = (int) Math.ceil((Math.log(input.length) / Math.log(2))); for (int i = 3; i < round; i++) { // minimal length=16 int segmentLen = 1 << i; mergeRound(segmentLen, input, output, mainPool); int[] temp = input; input = output; output = temp; } System.out.println("\n\tthread account: " + mainPool.getActiveThreadCount()); System.out.println("\tparallelism: " + mainPool.getParallelism()); System.out.println("\tsteal count: " + mainPool.getStealCount()); mainPool.shutdown(); return input; }