Package com.j3d.threads
Class TimeoutWorker
- All Implemented Interfaces:
Runnable,Future<Void>,RunnableFuture<Void>
A SwingWorker that acts as a countdown timer, executing a callback upon completion.
This worker runs a countdown from a specified start time on a background thread.
If the timer completes without being cancelled, it executes a provided Runnable
on the Event Dispatch Thread (EDT).
- Author:
- Lehlogonolo Poole
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class javax.swing.SwingWorker
SwingWorker.StateValueNested classes/interfaces inherited from interface java.util.concurrent.Future
Future.State -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionTimeoutWorker(int startTimeInSeconds, Runnable onFinishedCallback) Constructs a new TimeoutWorker. -
Method Summary
Methods inherited from class javax.swing.SwingWorker
addPropertyChangeListener, cancel, execute, firePropertyChange, get, get, getProgress, getPropertyChangeSupport, getState, isCancelled, isDone, process, publish, removePropertyChangeListener, run, setProgressMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.concurrent.Future
exceptionNow, resultNow, state
-
Field Details
-
startTimeInSeconds
private final int startTimeInSeconds -
onFinishedCallback
-
-
Constructor Details
-
TimeoutWorker
Constructs a new TimeoutWorker.- Parameters:
startTimeInSeconds- The number of seconds to count down from.onFinishedCallback- TheRunnableto execute on the EDT only if the timer finishes successfully.
-
-
Method Details
-
doInBackground
The main background task that performs the countdown.This method runs on a worker thread. It sleeps for one-second intervals and checks for cancellation on each iteration.
- Specified by:
doInBackgroundin classSwingWorker<Void,Void> - Returns:
- null, as this worker does not produce a final result.
- Throws:
Exception
-
done
protected void done()Called on the Event Dispatch Thread (EDT) after the background task finishes.This method checks if the worker was cancelled. If it was not, it executes the success callback.
- Overrides:
donein classSwingWorker<Void,Void>
-