diff --git a/hazelcast/src/main/java/com/hazelcast/util/executor/LoggingScheduledExecutor.java b/hazelcast/src/main/java/com/hazelcast/util/executor/LoggingScheduledExecutor.java index 40d58f2fc5bb..4b4f4116df81 100644 --- a/hazelcast/src/main/java/com/hazelcast/util/executor/LoggingScheduledExecutor.java +++ b/hazelcast/src/main/java/com/hazelcast/util/executor/LoggingScheduledExecutor.java @@ -66,12 +66,12 @@ public LoggingScheduledExecutor(ILogger logger, int corePoolSize, ThreadFactory @Override protected RunnableScheduledFuture decorateTask(Runnable runnable, RunnableScheduledFuture task) { - return new LoggingDelegatingFuture(runnable, task); + return new LoggingDelegatingFuture(runnable, task, this); } @Override protected RunnableScheduledFuture decorateTask(Callable callable, RunnableScheduledFuture task) { - return new LoggingDelegatingFuture(callable, task); + return new LoggingDelegatingFuture(callable, task, this); } @Override @@ -117,10 +117,12 @@ static class LoggingDelegatingFuture implements RunnableScheduledFuture { private final Object task; private final RunnableScheduledFuture delegate; + private final LoggingScheduledExecutor executor; - LoggingDelegatingFuture(Object task, RunnableScheduledFuture delegate) { + LoggingDelegatingFuture(Object task, RunnableScheduledFuture delegate, LoggingScheduledExecutor executor) { this.task = task; this.delegate = delegate; + this.executor = executor; } @Override @@ -162,7 +164,11 @@ public int hashCode() { @Override public boolean cancel(boolean mayInterruptIfRunning) { - return delegate.cancel(mayInterruptIfRunning); + boolean cancelled = delegate.cancel(mayInterruptIfRunning); + if (cancelled) { + executor.remove(this); + } + return cancelled; } @Override diff --git a/hazelcast/src/test/java/com/hazelcast/util/executor/LoggingScheduledExecutorTest.java b/hazelcast/src/test/java/com/hazelcast/util/executor/LoggingScheduledExecutorTest.java index 370fd669fff8..276d93e69a0e 100644 --- a/hazelcast/src/test/java/com/hazelcast/util/executor/LoggingScheduledExecutorTest.java +++ b/hazelcast/src/test/java/com/hazelcast/util/executor/LoggingScheduledExecutorTest.java @@ -32,6 +32,7 @@ import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; +import java.util.concurrent.BlockingQueue; import java.util.concurrent.Callable; import java.util.concurrent.CancellationException; import java.util.concurrent.CountDownLatch; @@ -41,6 +42,7 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import java.util.logging.Level; @@ -72,6 +74,27 @@ public void tearDown() throws Exception { } } + @Test + public void no_remaining_task_after_cancel() throws Exception { + executor = new LoggingScheduledExecutor(logger, 1, factory); + + for (int i = 0; i < 10; i++) { + Future future = executor.submit(new Callable() { + @Override + public Integer call() throws Exception { + TimeUnit.HOURS.sleep(1); + return null; + } + }); + + future.cancel(true); + } + + BlockingQueue workQueue = ((LoggingScheduledExecutor) executor).getQueue(); + + assertEquals(0, workQueue.size()); + } + @Test public void testConstructor_withRejectedExecutionHandler() { RejectedExecutionHandler handler = new RejectedExecutionHandler() {