Class TaskHandle<R>
- Type Parameters:
R- task result type
BackgroundTask.
A handle is single-use. Calling start() more than once is safe
but only the first call starts a worker thread. The task runs on the daemon
thread owned by an internal SimulationEngine; all
TaskListener callbacks run on Swing's event-dispatch thread.
Cancellation is cooperative. cancel() sets a flag immediately,
but the task must periodically inspect its TaskContext to finish
promptly. The result and failure accessors are safe to read from any thread
after completion because their backing fields are published with volatile
semantics.
-
Constructor Summary
ConstructorsConstructorDescriptionTaskHandle(BackgroundTask<R> task) Creates an unstarted handle with quiet, automatically running defaults.TaskHandle(BackgroundTask<R> task, SimulationEngineConfig config) Creates an unstarted handle with an explicit engine configuration. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(TaskListener<R> listener) Adds a listener if the same instance is not already registered.booleanawaitTermination(long timeoutMillis) Waits for the worker thread to finish.voidcancel()Requests cooperative cancellation.Returns the terminal outcome, ornullwhile execution is pending.doubleReturns the task result.Exposes the underlying engine for advanced integration with existing MDI simulation listeners and diagnostics.getState()booleanvoidremoveListener(TaskListener<R> listener) Removes a listener.voidstart()Starts this task unless it was already started.
-
Constructor Details
-
TaskHandle
Creates an unstarted handle with quiet, automatically running defaults. Periodic simulation refresh and heartbeat events are disabled; explicit task progress, messages, and refresh requests remain available.- Parameters:
task- computation to execute- Throws:
NullPointerException- iftaskis null
-
TaskHandle
Creates an unstarted handle with an explicit engine configuration.The configuration must use automatic running because a one-shot task has no separate READY/RUN user gesture. This restriction prevents a task from appearing to start while silently waiting in the simulation READY state.
- Parameters:
task- computation to executeconfig- engine scheduling and notification configuration; itsSimulationEngineConfig.autoRunfield must be true- Throws:
NullPointerException- if either argument is nullIllegalArgumentException- if automatic running is disabled
-
-
Method Details
-
start
public void start()Starts this task unless it was already started. -
cancel
public void cancel()Requests cooperative cancellation. Repeated calls are harmless. Cancellation beforestart()prevents the task body from running. -
addListener
Adds a listener if the same instance is not already registered. Registration does not replay events that occurred earlier.- Parameters:
listener- listener to add;nullis ignored
-
removeListener
Removes a listener. @param listener listener to remove -
getState
- Returns:
- current underlying engine state
-
getCompletionStatus
Returns the terminal outcome, ornullwhile execution is pending.- Returns:
- terminal status or
null
-
isCompleted
public boolean isCompleted()- Returns:
trueafter any terminal outcome has been published
-
getResult
Returns the task result. A successful task is permitted to returnnull; callers should inspectgetCompletionStatus()to distinguish that result from an incomplete or failed task.- Returns:
- result value, or
null
-
getFailure
- Returns:
- failure, or
nullunless completion status is FAILED
-
getElapsedSeconds
public double getElapsedSeconds()- Returns:
- elapsed worker time in seconds
-
awaitTermination
Waits for the worker thread to finish. This method does not flush pending EDT callbacks; use listener latches when a test must wait for UI delivery.- Parameters:
timeoutMillis- maximum wait, or zero to wait indefinitely- Returns:
trueif the worker has terminated- Throws:
InterruptedException- if interrupted while waiting
-
getSimulationEngine
Exposes the underlying engine for advanced integration with existing MDI simulation listeners and diagnostics. Application code should normally use this handle's typed API.- Returns:
- engine owned by this handle
-