Class TaskExecutor

java.lang.Object
javax.swing.SwingWorker<java.lang.Void,​java.lang.Void>
org.sm.smtools.application.concurrent.TaskExecutor
All Implemented Interfaces:
java.lang.Runnable, java.util.concurrent.Future<java.lang.Void>, java.util.concurrent.RunnableFuture<java.lang.Void>

public class TaskExecutor
extends javax.swing.SwingWorker<java.lang.Void,​java.lang.Void>
The TaskExecutor class provides a facility for concurrently executing a number of ATasks.

The class executes a certain number of tasks simultaneously, each one starting in its own thread.
It then waits until all tasks have completed.

Tasks are added via the addTask(ATask) or addTasks(ArrayList) methods; results can be collected via the finishTasks() method.

Once all tasks are defined, a user calls the TaskExecutor.execute() method; the class can only be used once. The current state of execution can be queried via the isBusy() method. Threading is executed with a fixed thread pool based on the number of available cores.

Progress of the tasks' executions is shown via an optional progress update glasspane.

Version:
08/02/2015
Author:
Sven Maerivoet
See Also:
ATask
  • Nested Class Summary

    Nested classes/interfaces inherited from class javax.swing.SwingWorker

    javax.swing.SwingWorker.StateValue
  • Constructor Summary

    Constructors
    Constructor Description
    TaskExecutor()
    Constructs a TaskExecutor object without a progress update glasspane.
    TaskExecutor​(JProgressUpdateGlassPane progressUpdateGlassPane)
    Constructs a TaskExecutor object with a specified progress update glasspane and a fixed thread pool based on the number of available cores.
  • Method Summary

    Modifier and Type Method Description
    void addTask​(ATask task)
    Adds a task to be executed.
    void addTasks​(java.util.ArrayList<ATask> tasks)
    Adds a series of tasks to be executed.
    protected java.lang.Void doInBackground()  
    protected void done()  
    protected void finishTasks()
    This method is called when all tasks have finished.
    int getNrOfThreadsToUse()
    Returns the number of threads that is used.
    java.util.ArrayList<ATask> getTasks()
    Returns the list with tasks.
    protected void initialise()
    Performs custom initialisation before tasks are executed.
    boolean isBusy()
    Returns whether or not the task executor is busy.
    protected void prepareTask​(ATask task)
    Allows preparation of a task before adding it to the internal task list.
    void setNrOfThreadsToUse​(int nrOfThreadsToUse)
    Sets the number of threads to use by creating a fixed thread pool.

    Methods inherited from class javax.swing.SwingWorker

    addPropertyChangeListener, cancel, execute, firePropertyChange, get, get, getProgress, getPropertyChangeSupport, getState, isCancelled, isDone, process, publish, removePropertyChangeListener, run, setProgress

    Methods inherited from class java.lang.Object

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

    • TaskExecutor

      public TaskExecutor​(JProgressUpdateGlassPane progressUpdateGlassPane)
      Constructs a TaskExecutor object with a specified progress update glasspane and a fixed thread pool based on the number of available cores.
      Parameters:
      progressUpdateGlassPane - the progress update glasspane to use for task updates
    • TaskExecutor

      public TaskExecutor()
      Constructs a TaskExecutor object without a progress update glasspane.
  • Method Details

    • setNrOfThreadsToUse

      public final void setNrOfThreadsToUse​(int nrOfThreadsToUse)
      Sets the number of threads to use by creating a fixed thread pool.

      Note that this number is bound by the number of available processor cores in the system.

      Parameters:
      nrOfThreadsToUse - the number of threads to use for the fixed thread pool
    • getNrOfThreadsToUse

      public final int getNrOfThreadsToUse()
      Returns the number of threads that is used.
      Returns:
      the number of threads that is used
    • addTask

      public final void addTask​(ATask task)
      Adds a task to be executed.
      Parameters:
      task - the task to be executed
    • addTasks

      public final void addTasks​(java.util.ArrayList<ATask> tasks)
      Adds a series of tasks to be executed.
      Parameters:
      tasks - the tasks to be executed
    • getTasks

      public final java.util.ArrayList<ATask> getTasks()
      Returns the list with tasks.
      Returns:
      the list with tasks
    • isBusy

      public final boolean isBusy()
      Returns whether or not the task executor is busy.
      Returns:
      a boolean indicating whether or not the task executor is busy
    • prepareTask

      protected void prepareTask​(ATask task)
      Allows preparation of a task before adding it to the internal task list.
      Parameters:
      task - the task to prepare
    • initialise

      protected void initialise()
      Performs custom initialisation before tasks are executed.
    • finishTasks

      protected void finishTasks()
      This method is called when all tasks have finished.
    • doInBackground

      protected final java.lang.Void doInBackground() throws java.lang.Exception
      Specified by:
      doInBackground in class javax.swing.SwingWorker<java.lang.Void,​java.lang.Void>
      Returns:
      -
      Throws:
      java.lang.Exception - -
    • done

      protected final void done()
      Overrides:
      done in class javax.swing.SwingWorker<java.lang.Void,​java.lang.Void>