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


Java ThreadPoolExecutor.getPoolSize方法代码示例

本文整理汇总了Java中java.util.concurrent.ThreadPoolExecutor.getPoolSize方法的典型用法代码示例。如果您正苦于以下问题:Java ThreadPoolExecutor.getPoolSize方法的具体用法?Java ThreadPoolExecutor.getPoolSize怎么用?Java ThreadPoolExecutor.getPoolSize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.concurrent.ThreadPoolExecutor的用法示例。


在下文中一共展示了ThreadPoolExecutor.getPoolSize方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testAllowCoreThreadTimeOut_true

import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * allowCoreThreadTimeOut(true) causes idle threads to time out
 */
public void testAllowCoreThreadTimeOut_true() throws Exception {
    long keepAliveTime = timeoutMillis();
    final ThreadPoolExecutor p =
        new ThreadPoolExecutor(2, 10,
                               keepAliveTime, MILLISECONDS,
                               new ArrayBlockingQueue<Runnable>(10));
    try (PoolCleaner cleaner = cleaner(p)) {
        final CountDownLatch threadStarted = new CountDownLatch(1);
        p.allowCoreThreadTimeOut(true);
        p.execute(new CheckedRunnable() {
            public void realRun() {
                threadStarted.countDown();
                assertEquals(1, p.getPoolSize());
            }});
        await(threadStarted);
        delay(keepAliveTime);
        long startTime = System.nanoTime();
        while (p.getPoolSize() > 0
               && millisElapsedSince(startTime) < LONG_DELAY_MS)
            Thread.yield();
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
        assertEquals(0, p.getPoolSize());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:28,代码来源:ThreadPoolExecutorTest.java

示例2: testAllowCoreThreadTimeOut_true

import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * allowCoreThreadTimeOut(true) causes idle threads to time out
 */
public void testAllowCoreThreadTimeOut_true() throws Exception {
    long keepAliveTime = timeoutMillis();
    final ThreadPoolExecutor p =
        new CustomTPE(2, 10,
                      keepAliveTime, MILLISECONDS,
                      new ArrayBlockingQueue<Runnable>(10));
    try (PoolCleaner cleaner = cleaner(p)) {
        final CountDownLatch threadStarted = new CountDownLatch(1);
        p.allowCoreThreadTimeOut(true);
        p.execute(new CheckedRunnable() {
            public void realRun() {
                threadStarted.countDown();
                assertEquals(1, p.getPoolSize());
            }});
        await(threadStarted);
        delay(keepAliveTime);
        long startTime = System.nanoTime();
        while (p.getPoolSize() > 0
               && millisElapsedSince(startTime) < LONG_DELAY_MS)
            Thread.yield();
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
        assertEquals(0, p.getPoolSize());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:28,代码来源:ThreadPoolExecutorSubclassTest.java

示例3: realMain

import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
private static void realMain(String[] args) throws Throwable {
    final int n = 4;
    final CyclicBarrier barrier = new CyclicBarrier(2*n+1);
    final ThreadPoolExecutor pool
        = new ThreadPoolExecutor(n, 2*n,
                                 KEEPALIVE_MS, MILLISECONDS,
                                 new SynchronousQueue<Runnable>());
    final Runnable r = new Runnable() { public void run() {
        try {
            barrier.await();
            barrier.await();
        } catch (Throwable t) { unexpected(t); }}};

    for (int i = 0; i < 2*n; i++)
        pool.execute(r);
    barrier.await();
    checkPoolSizes(pool, 2*n, n, 2*n);
    barrier.await();
    long nap = KEEPALIVE_MS + (KEEPALIVE_MS >> 2);
    for (long sleepyTime = 0L; pool.getPoolSize() > n; ) {
        check((sleepyTime += nap) <= LONG_DELAY_MS);
        Thread.sleep(nap);
    }
    checkPoolSizes(pool, n, n, 2*n);
    Thread.sleep(nap);
    checkPoolSizes(pool, n, n, 2*n);
    pool.shutdown();
    check(pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:30,代码来源:TimeOutShrink.java

示例4: awaitPoolSize

import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
static void awaitPoolSize(ThreadPoolExecutor pool, int n) {
    while (pool.getPoolSize() != n) Thread.yield();
    pass();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:5,代码来源:ModifyCorePoolSize.java


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