Class Worker<T>

java.lang.Object
org.monte.media.swing.Worker<T>
All Implemented Interfaces:
Runnable

public abstract class Worker<T> extends Object implements Runnable
This is an abstract class that you can subclass to perform GUI-related work in a dedicated event dispatcher.

This class is similar to SwingWorker but less complex.

Author:
Werner Randelshofer
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected abstract T
    Compute the value to be returned by the get method.
    protected void
    done(T value)
    Called on the event dispatching thread (not on the worker thread) after the construct method has returned without throwing an error.
    protected void
    Called on the event dispatching thread (not on the worker thread) after the construct method has thrown an error.
    protected void
    Called on the event dispatching thread (not on the worker thread) after the construct method has finished and after done() or failed() has been invoked.
    protected Throwable
    Get the error produced by the worker thread, or null if it hasn't thrown one.
    Get the value produced by the worker thread, or null if it hasn't been constructed yet.
    final void
    run()
    Calls #construct on the current thread and invokes #done on the AWT event dispatcher thread.
    void
    Starts the Worker on an internal worker thread.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Worker

      public Worker()
  • Method Details

    • run

      public final void run()
      Calls #construct on the current thread and invokes #done on the AWT event dispatcher thread.
      Specified by:
      run in interface Runnable
    • construct

      protected abstract T construct() throws Exception
      Compute the value to be returned by the get method.
      Throws:
      Exception
    • done

      protected void done(T value)
      Called on the event dispatching thread (not on the worker thread) after the construct method has returned without throwing an error.

      The default implementation does nothing. Subclasses may override this method to perform done actions on the Event Dispatch Thread.

      Parameters:
      value - The return value of the construct method.
    • failed

      protected void failed(Throwable error)
      Called on the event dispatching thread (not on the worker thread) after the construct method has thrown an error.

      The default implementation prints a stack trace. Subclasses may override this method to perform failure actions on the Event Dispatch Thread.

      Parameters:
      error - The error thrown by construct.
    • finished

      protected void finished()
      Called on the event dispatching thread (not on the worker thread) after the construct method has finished and after done() or failed() has been invoked.

      The default implementation does nothing. Subclasses may override this method to perform completion actions on the Event Dispatch Thread.

    • getValue

      public T getValue()
      Get the value produced by the worker thread, or null if it hasn't been constructed yet.
    • getError

      protected Throwable getError()
      Get the error produced by the worker thread, or null if it hasn't thrown one.
    • start

      public void start()
      Starts the Worker on an internal worker thread.