Class TaskExecutor

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

public class TaskExecutor extends SwingWorker<Void,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:
  • 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(ArrayList<ATask> tasks)
      Adds a series of tasks to be executed.
      Parameters:
      tasks - the tasks to be executed
    • getTasks

      public final 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 Void doInBackground() throws Exception
      Specified by:
      doInBackground in class SwingWorker<Void,Void>
      Returns:
      -
      Throws:
      Exception - -
    • done

      protected final void done()
      Overrides:
      done in class SwingWorker<Void,Void>