Java Code Examples for java.util.concurrent.ScheduledThreadPoolExecutor#shutdownNow()
The following examples show how to use
java.util.concurrent.ScheduledThreadPoolExecutor#shutdownNow() .
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: ThreadRestarts.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
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())); }
Example 2
Source File: RunnableWrapperTest.java From servicecomb-java-chassis with Apache License 2.0 | 5 votes |
/** * {@link SuppressedRunnableWrapper} should ensure that any {@link Throwable} thrown from {@link Runnable} * should not interrupt the scheduled tasks. */ @Test public void run() { ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1); CountDownLatch countDownLatch = new CountDownLatch(3); try { scheduledThreadPoolExecutor.scheduleAtFixedRate( new SuppressedRunnableWrapper( () -> { countDownLatch.countDown(); switch ((int) countDownLatch.getCount()) { case 2: throw new Error("Normal case, this is a mocked error"); case 1: throw new IllegalStateException("Normal case, this is a mocked exception"); default: } } ), 1, 1, TimeUnit.MILLISECONDS); countDownLatch.await(1, TimeUnit.SECONDS); } catch (InterruptedException e) { System.out.println("get an InterruptedException! " + e.getMessage()); e.printStackTrace(); } finally { scheduledThreadPoolExecutor.shutdownNow(); } assertEquals(0, countDownLatch.getCount()); }
Example 3
Source File: ScheduledExecutorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * 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()); }
Example 4
Source File: ScheduledExecutorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * 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()); }
Example 5
Source File: ModifyCorePoolSize.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
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)); }
Example 6
Source File: ZeroCoreThreads.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
void test(String[] args) throws Throwable { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(0); try { test(p); } finally { p.shutdownNow(); check(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); } }
Example 7
Source File: ClientThreadPoolsTest.java From activemq-artemis with Apache License 2.0 | 5 votes |
@Test public void testInjectPools() throws Exception { ActiveMQClient.clearThreadPools(); ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(1, ActiveMQThreadFactory.defaultThreadFactory()); ActiveMQClient.injectPools(poolExecutor, scheduledThreadPoolExecutor); final CountDownLatch inUse = new CountDownLatch(1); final CountDownLatch neverLeave = new CountDownLatch(1); ActiveMQClient.getGlobalThreadPool().execute(new Runnable() { @Override public void run() { try { inUse.countDown(); neverLeave.await(); } catch (Exception e) { e.printStackTrace(); neverLeave.countDown(); } } }); Assert.assertTrue(inUse.await(10, TimeUnit.SECONDS)); poolExecutor.shutdownNow(); scheduledThreadPoolExecutor.shutdownNow(); Assert.assertTrue(neverLeave.await(10, TimeUnit.SECONDS)); Assert.assertTrue(inUse.await(10, TimeUnit.SECONDS)); Assert.assertTrue(neverLeave.await(10, TimeUnit.SECONDS)); ActiveMQClient.clearThreadPools(100, TimeUnit.MILLISECONDS); }
Example 8
Source File: ScheduledDeliveryHandlerTest.java From activemq-artemis with Apache License 2.0 | 5 votes |
@Test public void testScheduleNow() throws Exception { ExecutorService executor = Executors.newFixedThreadPool(50, ActiveMQThreadFactory.defaultThreadFactory()); ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(1, ActiveMQThreadFactory.defaultThreadFactory()); try { for (int i = 0; i < 100; i++) { // it's better to run the test a few times instead of run millions of messages here internalSchedule(executor, scheduler); } } finally { scheduler.shutdownNow(); executor.shutdownNow(); } }
Example 9
Source File: ScheduledExecutorTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * 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()); }
Example 10
Source File: ScheduledExecutorTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * 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()); }
Example 11
Source File: Scheduler.java From crate with Apache License 2.0 | 5 votes |
static boolean terminate(ScheduledThreadPoolExecutor scheduledThreadPoolExecutor, long timeout, TimeUnit timeUnit) { scheduledThreadPoolExecutor.shutdown(); if (awaitTermination(scheduledThreadPoolExecutor, timeout, timeUnit)) { return true; } // last resort scheduledThreadPoolExecutor.shutdownNow(); return awaitTermination(scheduledThreadPoolExecutor, timeout, timeUnit); }