本文整理汇总了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());
}
}
示例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());
}
}
示例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));
}
示例4: awaitPoolSize
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
static void awaitPoolSize(ThreadPoolExecutor pool, int n) {
while (pool.getPoolSize() != n) Thread.yield();
pass();
}