Java Code Examples for java.util.concurrent.ScheduledThreadPoolExecutor#purge()
The following examples show how to use
java.util.concurrent.ScheduledThreadPoolExecutor#purge() .
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: ScheduledExecutorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * purge eventually removes cancelled tasks from the queue */ public void testPurge() throws InterruptedException { final ScheduledFuture[] tasks = new ScheduledFuture[5]; final Runnable releaser = new Runnable() { public void run() { for (ScheduledFuture task : tasks) if (task != null) task.cancel(true); }}; final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); try (PoolCleaner cleaner = cleaner(p, releaser)) { for (int i = 0; i < tasks.length; i++) tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), LONG_DELAY_MS, MILLISECONDS); int max = tasks.length; if (tasks[4].cancel(true)) --max; if (tasks[3].cancel(true)) --max; // There must eventually be an interference-free point at // which purge will not fail. (At worst, when queue is empty.) long startTime = System.nanoTime(); do { p.purge(); long count = p.getTaskCount(); if (count == max) return; } while (millisElapsedSince(startTime) < LONG_DELAY_MS); fail("Purge failed to remove cancelled tasks"); } }
Example 2
Source File: ScheduledExecutorTest.java From j2objc with Apache License 2.0 | 6 votes |
/** * purge eventually removes cancelled tasks from the queue */ public void testPurge() throws InterruptedException { final ScheduledFuture[] tasks = new ScheduledFuture[5]; final Runnable releaser = new Runnable() { public void run() { for (ScheduledFuture task : tasks) if (task != null) task.cancel(true); }}; final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); try (PoolCleaner cleaner = cleaner(p, releaser)) { for (int i = 0; i < tasks.length; i++) tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), LONG_DELAY_MS, MILLISECONDS); int max = tasks.length; if (tasks[4].cancel(true)) --max; if (tasks[3].cancel(true)) --max; // There must eventually be an interference-free point at // which purge will not fail. (At worst, when queue is empty.) long startTime = System.nanoTime(); do { p.purge(); long count = p.getTaskCount(); if (count == max) return; } while (millisElapsedSince(startTime) < LONG_DELAY_MS); fail("Purge failed to remove cancelled tasks"); } }
Example 3
Source File: X509Ca.java From xipki with Apache License 2.0 | 6 votes |
@Override public void close() { if (crlGenerationService != null) { crlGenerationService.cancel(false); crlGenerationService = null; } if (expiredCertsRemover != null) { expiredCertsRemover.cancel(false); expiredCertsRemover = null; } if (suspendedCertsRevoker != null) { suspendedCertsRevoker.cancel(false); suspendedCertsRevoker = null; } ScheduledThreadPoolExecutor executor = caManager.getScheduledThreadPoolExecutor(); if (executor != null) { executor.purge(); } }
Example 4
Source File: ContextTest.java From grpc-nebula-java with Apache License 2.0 | 5 votes |
@Test public void cancellationCancelsScheduledTask() { ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1); try { assertEquals(0, executor.getQueue().size()); Context.CancellableContext base = Context.current().withDeadline(Deadline.after(1, TimeUnit.DAYS), executor); assertEquals(1, executor.getQueue().size()); base.cancel(null); executor.purge(); assertEquals(0, executor.getQueue().size()); } finally { executor.shutdown(); } }
Example 5
Source File: ContextTest.java From grpc-java with Apache License 2.0 | 5 votes |
@Test public void cancellationCancelsScheduledTask() { ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1); try { assertEquals(0, executor.getQueue().size()); Context.CancellableContext base = Context.current().withDeadline(Deadline.after(1, TimeUnit.DAYS), executor); assertEquals(1, executor.getQueue().size()); base.cancel(null); executor.purge(); assertEquals(0, executor.getQueue().size()); } finally { executor.shutdown(); } }
Example 6
Source File: ServiceBackgroundTaskManager.java From sepia-assist-server with MIT License | 4 votes |
/** * Run a given task in background and manage task references and threads. * @param userId * @param serviceId * @param delayMs * @param task * @return */ public static ServiceBackgroundTask runOnceInBackground(String userId, String serviceId, long delayMs, Runnable task){ //TODO: introduce max. delay ? String taskId = getNewTaskId(); int corePoolSize = 1; final ScheduledThreadPoolExecutor executor = ThreadManager.getNewScheduledThreadPool(corePoolSize); executor.setRemoveOnCancelPolicy(true); ScheduledFuture<?> future = executor.schedule(() -> { //run task and... try{ increaseThreadCounter(); task.run(); decreaseThreadCounter(); }catch (Exception e){ decreaseThreadCounter(); } //... remove yourself from manager removeFromSbtMap(taskId); executor.purge(); executor.shutdown(); }, delayMs, TimeUnit.MILLISECONDS); //other option (but does not support lambda expression): //Timer timer = new Timer(); //timer.schedule(task, delayMs); BooleanSupplier cancelFun = () -> { if (future.isDone() || future.cancel(false)){ removeFromSbtMap(taskId); executor.purge(); executor.shutdown(); return true; }else{ executor.purge(); executor.shutdown(); return false; } }; ServiceBackgroundTask sbt = new ServiceBackgroundTask(serviceId, taskId, future, cancelFun); addToSbtMap(taskId, sbt); return sbt; }