Class CoalescingTaskScheduler
java.lang.Object
org.jjazz.utilities.api.CoalescingTaskScheduler
Call a task after a delay.
- User can call request(Runnable task) repeatedly
- Runs the *last* Runnable only, after delayMs without new requests (debounce mode) or within a fixed window (throttle mode)
By default, the Runnable executes on the scheduler thread. If you need EDT execution, wrap the Runnable: () -> SwingUtilities.invokeLater(...).
Thread-safe: all public methods can be called from any thread.
-
Constructor Summary
ConstructorsConstructorDescriptionCoalescingTaskScheduler(long delayMs) Create a CoalescingTaskScheduler with default settings (debounce mode, logging exceptions).CoalescingTaskScheduler(long delayMs, boolean restartDelayOnNewRequest, Consumer<Throwable> exceptionHandler) Create a CoalescingTaskScheduler with full configuration.CoalescingTaskScheduler(long delayMs, Consumer<Throwable> exceptionHandler) Create a CoalescingTaskScheduler with custom exception handling. -
Method Summary
Modifier and TypeMethodDescriptionvoidcancel()Cancel any pending action.longGet the configured delay in milliseconds.booleanCheck if there is a task currently scheduled to run.booleanCheck if the scheduler is in debounce mode (restarting delay on each request).voidSchedule action to run after delayMs.
-
Constructor Details
-
CoalescingTaskScheduler
public CoalescingTaskScheduler(long delayMs) Create a CoalescingTaskScheduler with default settings (debounce mode, logging exceptions).- Parameters:
delayMs- The delay in milliseconds before executing the task- Throws:
IllegalArgumentException- if delayMs is negative
-
CoalescingTaskScheduler
Create a CoalescingTaskScheduler with custom exception handling.- Parameters:
delayMs- The delay in milliseconds before executing the taskexceptionHandler- Optional handler for exceptions thrown by scheduled tasks. If null, exceptions are logged at WARNING level.- Throws:
IllegalArgumentException- if delayMs is negative
-
CoalescingTaskScheduler
public CoalescingTaskScheduler(long delayMs, boolean restartDelayOnNewRequest, Consumer<Throwable> exceptionHandler) Create a CoalescingTaskScheduler with full configuration.- Parameters:
delayMs- The delay in milliseconds before executing the taskrestartDelayOnNewRequest- If true (debounce mode): each new request resets the delay timer. If false (throttle mode): the first request starts a fixed delay window, subsequent requests only update the task to run.exceptionHandler- Optional handler for exceptions thrown by scheduled tasks. If null, exceptions are logged at WARNING level.
-
-
Method Details
-
request
Schedule action to run after delayMs.Any exception thrown by the action will be handled by the configured exception handler, or logged if no handler was provided.
- Parameters:
action- The task to execute
-
cancel
public void cancel()Cancel any pending action.After this call, no task will execute unless request() is called again.
-
hasPendingTask
public boolean hasPendingTask()Check if there is a task currently scheduled to run.Note: This method returns a snapshot of the current state. By the time the caller acts on this information, the state may have changed.
- Returns:
- true if a task is scheduled and has not yet executed or been cancelled
-
getDelayMs
public long getDelayMs()Get the configured delay in milliseconds.- Returns:
- the delay before task execution
-
isRestartDelayOnNewRequest
public boolean isRestartDelayOnNewRequest()Check if the scheduler is in debounce mode (restarting delay on each request).- Returns:
-