本文整理汇总了Java中java.util.concurrent.ScheduledThreadPoolExecutor.shutdownNow方法的典型用法代码示例。如果您正苦于以下问题:Java ScheduledThreadPoolExecutor.shutdownNow方法的具体用法?Java ScheduledThreadPoolExecutor.shutdownNow怎么用?Java ScheduledThreadPoolExecutor.shutdownNow使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ScheduledThreadPoolExecutor
的用法示例。
在下文中一共展示了ScheduledThreadPoolExecutor.shutdownNow方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test
import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
private static void test(boolean allowTimeout) throws Exception {
CountingThreadFactory ctf = new CountingThreadFactory();
ScheduledThreadPoolExecutor stpe
= new ScheduledThreadPoolExecutor(10, ctf);
try {
// schedule a dummy task in the "far future"
Runnable nop = new Runnable() { public void run() {}};
stpe.schedule(nop, FAR_FUTURE_MS, MILLISECONDS);
stpe.setKeepAliveTime(1L, MILLISECONDS);
stpe.allowCoreThreadTimeOut(allowTimeout);
MILLISECONDS.sleep(12L);
} finally {
stpe.shutdownNow();
if (!stpe.awaitTermination(LONG_DELAY_MS, MILLISECONDS))
throw new AssertionError("timed out");
}
if (ctf.count.get() > 1)
throw new AssertionError(
String.format("%d threads created, 1 expected",
ctf.count.get()));
}
示例2: testShutdownNow
import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
/**
* shutdownNow returns a list containing tasks that were not run,
* and those tasks are drained from the queue
*/
public void testShutdownNow() throws InterruptedException {
final int poolSize = 2;
final int count = 5;
final AtomicInteger ran = new AtomicInteger(0);
final ScheduledThreadPoolExecutor p =
new ScheduledThreadPoolExecutor(poolSize);
final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
Runnable waiter = new CheckedRunnable() { public void realRun() {
threadsStarted.countDown();
try {
MILLISECONDS.sleep(2 * LONG_DELAY_MS);
} catch (InterruptedException success) {}
ran.getAndIncrement();
}};
for (int i = 0; i < count; i++)
p.execute(waiter);
await(threadsStarted);
assertEquals(poolSize, p.getActiveCount());
assertEquals(0, p.getCompletedTaskCount());
final List<Runnable> queuedTasks;
try {
queuedTasks = p.shutdownNow();
} catch (SecurityException ok) {
return; // Allowed in case test doesn't have privs
}
assertTrue(p.isShutdown());
assertTrue(p.getQueue().isEmpty());
assertEquals(count - poolSize, queuedTasks.size());
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
assertTrue(p.isTerminated());
assertEquals(poolSize, ran.get());
assertEquals(poolSize, p.getCompletedTaskCount());
}
示例3: testShutdownNow_delayedTasks
import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
/**
* shutdownNow returns a list containing tasks that were not run,
* and those tasks are drained from the queue
*/
public void testShutdownNow_delayedTasks() throws InterruptedException {
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
List<ScheduledFuture> tasks = new ArrayList<>();
for (int i = 0; i < 3; i++) {
Runnable r = new NoOpRunnable();
tasks.add(p.schedule(r, 9, SECONDS));
tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS));
tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS));
}
if (testImplementationDetails)
assertEquals(new HashSet(tasks), new HashSet(p.getQueue()));
final List<Runnable> queuedTasks;
try {
queuedTasks = p.shutdownNow();
} catch (SecurityException ok) {
return; // Allowed in case test doesn't have privs
}
assertTrue(p.isShutdown());
assertTrue(p.getQueue().isEmpty());
if (testImplementationDetails)
assertEquals(new HashSet(tasks), new HashSet(queuedTasks));
assertEquals(tasks.size(), queuedTasks.size());
for (ScheduledFuture task : tasks) {
assertFalse(task.isDone());
assertFalse(task.isCancelled());
}
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
assertTrue(p.isTerminated());
}
示例4: realMain
import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
static void realMain(String[] args) throws Throwable {
final int size = 10;
final ScheduledThreadPoolExecutor pool
= new ScheduledThreadPoolExecutor(size);
final Runnable nop = new Runnable() { public void run() {}};
for (int i = 0; i < size; i++)
pool.scheduleAtFixedRate(nop, 100L * (i + 1),
1000L, TimeUnit.MILLISECONDS);
awaitPoolSize(pool, size);
setCorePoolSize(pool, size - 3);
setCorePoolSize(pool, size + 3);
pool.shutdownNow();
check(pool.awaitTermination(1L, TimeUnit.DAYS));
}
示例5: test
import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
void test(String[] args) throws Throwable {
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(0);
try {
test(p);
} finally {
p.shutdownNow();
check(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
}
}