From 338454a63f23735f75d34dd91c17f867f378a306 Mon Sep 17 00:00:00 2001 From: Radu Sebastian LAZIN Date: Fri, 4 Dec 2026 15:19:19 +0200 Subject: [PATCH 01/15] Isolate rescheduling task lifecycle and execution ownership --- .../morphix/lang/thread/ReschedulingTask.java | 139 +++++++++++------- 1 file changed, 86 insertions(+), 53 deletions(-) diff --git a/src/main/java/org/morphix/lang/thread/ReschedulingTask.java b/src/main/java/org/morphix/lang/thread/ReschedulingTask.java index a540dcdf..da439e97 100644 --- a/src/main/java/org/morphix/lang/thread/ReschedulingTask.java +++ b/src/main/java/org/morphix/lang/thread/ReschedulingTask.java @@ -17,6 +17,7 @@ import java.util.Objects; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @@ -38,7 +39,8 @@ *

* The task can be enabled or disabled, and it will manage its own scheduling and cancellation to ensure that only one * instance of the task is scheduled at any time. The scheduling is done after the current task execution finishes, so - * if the task takes longer than the delay, it will not cause overlapping executions. + * executions within one activation do not overlap. Disabling and enabling again may overlap with an execution from + * a previous activation that has not yet finished, but that execution cannot reschedule into the new activation. *

* Enabling the task will start the task execution on the same thread that called {@link #enable()} and rescheduling * will be done on a separate thread managed by the provided {@link ScheduledExecutorService}. Disabling it will stop @@ -76,6 +78,30 @@ private enum State { CLOSED } + /** + * Unique activation of the task. Scheduled executions belong only to this activation. + */ + private static final class Lifecycle { + + private final State state; + private final AtomicReference execution = new AtomicReference<>(); + + private Lifecycle(final State state) { + this.state = state; + } + } + + /** + * Scheduling identity published before submitting the callback to the executor. + */ + private static final class Execution { + + private final AtomicReference> future = new AtomicReference<>(); + } + + private static final Lifecycle DISABLED = new Lifecycle(State.DISABLED); + private static final Lifecycle CLOSED = new Lifecycle(State.CLOSED); + /** * Name space class for default values. Other defaults which are not constants: *