Java Code Examples for java.util.concurrent.ForkJoinPool#getActiveThreadCount()

The following examples show how to use java.util.concurrent.ForkJoinPool#getActiveThreadCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ForkJoinPoolTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 2
Source File: ForkJoinPoolTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 3
Source File: ForkJoinPool8Test.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * awaitQuiescence by a worker is equivalent in effect to
 * ForkJoinTask.helpQuiesce()
 */
public void testAwaitQuiescence1() throws Exception {
    final ForkJoinPool p = new ForkJoinPool();
    try (PoolCleaner cleaner = cleaner(p)) {
        final long startTime = System.nanoTime();
        assertTrue(p.isQuiescent());
        ForkJoinTask a = new CheckedRecursiveAction() {
            protected void realCompute() {
                FibAction f = new FibAction(8);
                assertSame(f, f.fork());
                assertSame(p, ForkJoinTask.getPool());
                boolean quiescent = p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS);
                assertTrue(quiescent);
                assertFalse(p.isQuiescent());
                while (!f.isDone()) {
                    assertFalse(p.getAsyncMode());
                    assertFalse(p.isShutdown());
                    assertFalse(p.isTerminating());
                    assertFalse(p.isTerminated());
                    Thread.yield();
                }
                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
                assertFalse(p.isQuiescent());
                assertEquals(0, ForkJoinTask.getQueuedTaskCount());
                assertEquals(21, f.result);
            }};
        p.execute(a);
        while (!a.isDone() || !p.isQuiescent()) {
            assertFalse(p.getAsyncMode());
            assertFalse(p.isShutdown());
            assertFalse(p.isTerminating());
            assertFalse(p.isTerminated());
            Thread.yield();
        }
        assertEquals(0, p.getQueuedTaskCount());
        assertFalse(p.getAsyncMode());
        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(millisElapsedSince(startTime) < LONG_DELAY_MS);
    }
}
 
Example 4
Source File: ForkJoinPool8Test.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * awaitQuiescence returns when pool isQuiescent() or the indicated
 * timeout elapsed
 */
public void testAwaitQuiescence2() throws Exception {
    /**
     * """It is possible to disable or limit the use of threads in the
     * common pool by setting the parallelism property to zero. However
     * doing so may cause unjoined tasks to never be executed."""
     */
    if ("0".equals(System.getProperty(
         "java.util.concurrent.ForkJoinPool.common.parallelism")))
        return;
    final ForkJoinPool p = new ForkJoinPool();
    try (PoolCleaner cleaner = cleaner(p)) {
        assertTrue(p.isQuiescent());
        final long startTime = System.nanoTime();
        ForkJoinTask a = new CheckedRecursiveAction() {
            protected void realCompute() {
                FibAction f = new FibAction(8);
                assertSame(f, f.fork());
                while (!f.isDone()
                       && millisElapsedSince(startTime) < LONG_DELAY_MS) {
                    assertFalse(p.getAsyncMode());
                    assertFalse(p.isShutdown());
                    assertFalse(p.isTerminating());
                    assertFalse(p.isTerminated());
                    Thread.yield();
                }
                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
                assertEquals(0, ForkJoinTask.getQueuedTaskCount());
                assertEquals(21, f.result);
            }};
        p.execute(a);
        assertTrue(p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS));
        assertTrue(p.isQuiescent());
        assertTrue(a.isDone());
        assertEquals(0, p.getQueuedTaskCount());
        assertFalse(p.getAsyncMode());
        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(millisElapsedSince(startTime) < LONG_DELAY_MS);
    }
}
 
Example 5
Source File: ForkJoinPool8Test.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * awaitQuiescence by a worker is equivalent in effect to
 * ForkJoinTask.helpQuiesce()
 */
public void testAwaitQuiescence1() throws Exception {
    final ForkJoinPool p = new ForkJoinPool();
    try (PoolCleaner cleaner = cleaner(p)) {
        final long startTime = System.nanoTime();
        assertTrue(p.isQuiescent());
        ForkJoinTask a = new CheckedRecursiveAction() {
            protected void realCompute() {
                FibAction f = new FibAction(8);
                assertSame(f, f.fork());
                assertSame(p, ForkJoinTask.getPool());
                boolean quiescent = p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS);
                assertTrue(quiescent);
                assertFalse(p.isQuiescent());
                while (!f.isDone()) {
                    assertFalse(p.getAsyncMode());
                    assertFalse(p.isShutdown());
                    assertFalse(p.isTerminating());
                    assertFalse(p.isTerminated());
                    Thread.yield();
                }
                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
                assertFalse(p.isQuiescent());
                assertEquals(0, ForkJoinTask.getQueuedTaskCount());
                assertEquals(21, f.result);
            }};
        p.execute(a);
        while (!a.isDone() || !p.isQuiescent()) {
            assertFalse(p.getAsyncMode());
            assertFalse(p.isShutdown());
            assertFalse(p.isTerminating());
            assertFalse(p.isTerminated());
            Thread.yield();
        }
        assertEquals(0, p.getQueuedTaskCount());
        assertFalse(p.getAsyncMode());
        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(millisElapsedSince(startTime) < LONG_DELAY_MS);
    }
}
 
Example 6
Source File: ForkJoinPool8Test.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * awaitQuiescence returns when pool isQuiescent() or the indicated
 * timeout elapsed
 */
public void testAwaitQuiescence2() throws Exception {
    /**
     * """It is possible to disable or limit the use of threads in the
     * common pool by setting the parallelism property to zero. However
     * doing so may cause unjoined tasks to never be executed."""
     */
    if ("0".equals(System.getProperty(
         "java.util.concurrent.ForkJoinPool.common.parallelism")))
        return;
    final ForkJoinPool p = new ForkJoinPool();
    try (PoolCleaner cleaner = cleaner(p)) {
        assertTrue(p.isQuiescent());
        final long startTime = System.nanoTime();
        ForkJoinTask a = new CheckedRecursiveAction() {
            protected void realCompute() {
                FibAction f = new FibAction(8);
                assertSame(f, f.fork());
                while (!f.isDone()
                       && millisElapsedSince(startTime) < LONG_DELAY_MS) {
                    assertFalse(p.getAsyncMode());
                    assertFalse(p.isShutdown());
                    assertFalse(p.isTerminating());
                    assertFalse(p.isTerminated());
                    Thread.yield();
                }
                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
                assertEquals(0, ForkJoinTask.getQueuedTaskCount());
                assertEquals(21, f.result);
            }};
        p.execute(a);
        assertTrue(p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS));
        assertTrue(p.isQuiescent());
        assertTrue(a.isDone());
        assertEquals(0, p.getQueuedTaskCount());
        assertFalse(p.getAsyncMode());
        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(millisElapsedSince(startTime) < LONG_DELAY_MS);
    }
}