Class TimeoutWorker

java.lang.Object
javax.swing.SwingWorker<Void,Void>
com.j3d.threads.TimeoutWorker
All Implemented Interfaces:
Runnable, Future<Void>, RunnableFuture<Void>

public class TimeoutWorker extends SwingWorker<Void,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:
  • Field Details

    • startTimeInSeconds

      private final int startTimeInSeconds
    • onFinishedCallback

      private final Runnable onFinishedCallback
  • Constructor Details

    • TimeoutWorker

      public TimeoutWorker(int startTimeInSeconds, Runnable onFinishedCallback)
      Constructs a new TimeoutWorker.
      Parameters:
      startTimeInSeconds - The number of seconds to count down from.
      onFinishedCallback - The Runnable to execute on the EDT only if the timer finishes successfully.
  • Method Details

    • doInBackground

      protected Void doInBackground() throws Exception
      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:
      doInBackground in class SwingWorker<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:
      done in class SwingWorker<Void,Void>