From 333d4735b51c22ed3617d4d66e197e245c2c947f Mon Sep 17 00:00:00 2001 From: "David M. Lloyd" Date: Tue, 13 Feb 2024 08:27:07 -0600 Subject: [PATCH] Avoid retaining references after successful cancelation Fixes #139 --- .../jboss/threads/EnhancedQueueExecutor.java | 40 ++++++++++++++++--- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/jboss/threads/EnhancedQueueExecutor.java b/src/main/java/org/jboss/threads/EnhancedQueueExecutor.java index 1f9b110..a3139bb 100644 --- a/src/main/java/org/jboss/threads/EnhancedQueueExecutor.java +++ b/src/main/java/org/jboss/threads/EnhancedQueueExecutor.java @@ -2614,6 +2614,7 @@ public boolean cancel(final boolean mayInterruptIfRunning) { case ASF_ST_SUBMITTED: { this.state = ASF_ST_CANCELLED; notifyAll(); + doCancel(); return true; } case ASF_ST_RUNNING: { @@ -2632,6 +2633,9 @@ public boolean cancel(final boolean mayInterruptIfRunning) { } } + void doCancel() { + } + @SuppressWarnings("unchecked") public V get() throws InterruptedException, ExecutionException { int state; @@ -2869,7 +2873,7 @@ static int wrongType() throws ClassCastException { } final class RunnableScheduledFuture extends AbstractScheduledFuture { - final Runnable runnable; + Runnable runnable; RunnableScheduledFuture(final Runnable runnable, final long delay, final TimeUnit unit) { super(delay, unit); @@ -2877,17 +2881,25 @@ final class RunnableScheduledFuture extends AbstractScheduledFuture { } Void performTask() { - runnable.run(); + try { + runnable.run(); + } finally { + runnable = null; + } return null; } + void doCancel() { + runnable = null; + } + StringBuilder toString(final StringBuilder b) { return super.toString(b).append(runnable); } } final class CallableScheduledFuture extends AbstractScheduledFuture { - final Callable callable; + Callable callable; CallableScheduledFuture(final Callable callable, final long delay, final TimeUnit unit) { super(delay, unit); @@ -2895,7 +2907,15 @@ final class CallableScheduledFuture extends AbstractScheduledFuture { } V performTask() throws Exception { - return callable.call(); + try { + return callable.call(); + } finally { + callable = null; + } + } + + void doCancel() { + callable = null; } StringBuilder toString(final StringBuilder b) { @@ -2945,7 +2965,7 @@ StringBuilder toString(final StringBuilder b) { } final class FixedRateRunnableScheduledFuture extends RepeatingScheduledFuture { - final Runnable runnable; + Runnable runnable; FixedRateRunnableScheduledFuture(final Runnable runnable, final long delay, final long period, final TimeUnit unit) { super(delay, period, unit); @@ -2962,13 +2982,17 @@ Void performTask() { return null; } + void doCancel() { + runnable = null; + } + StringBuilder toString(final StringBuilder b) { return super.toString(b).append(runnable); } } final class FixedDelayRunnableScheduledFuture extends RepeatingScheduledFuture { - final Runnable runnable; + Runnable runnable; FixedDelayRunnableScheduledFuture(final Runnable runnable, final long delay, final long period, final TimeUnit unit) { super(delay, period, unit); @@ -2984,6 +3008,10 @@ Void performTask() { return null; } + void doCancel() { + runnable = null; + } + StringBuilder toString(final StringBuilder b) { return super.toString(b).append(runnable); }