Class Executor

java.lang.Object
jadex.concurrent.Executor
All Implemented Interfaces:
Runnable

public class Executor extends Object implements Runnable
A helper class for running a single instance of code using the thread pool. The code to be executed has to be placed in the code() method. Once created, the execute() method may be called as often as desired. When no thread is currently executing the object, a new thread is used from the thread pool. Otherwise, the already existing thread continues execution. After shutdown() is called, the executor stops execution, even when execute() is called afterwards.
  • Field Details

    • EXECUTOR

      public static final ThreadLocal<Executor> EXECUTOR
      The executor belonging to a thread.
    • running

      protected boolean running
      Flag indicating if the thread is running.
    • wanttorun

      protected boolean wanttorun
      Flag indicating if the thread wants to run.
    • threadpool

      protected IThreadPool threadpool
      The thread pool.
    • executable

      protected IExecutable executable
      The executable.
    • shutdownfutures

      protected List<jadex.future.Future<Void>> shutdownfutures
      The shutdown futures.
    • monitor

      protected Object monitor
      The monitor to synchronize with at thread start (if any).
    • exethreadcnt

      protected int exethreadcnt
      The number of current threads for this executor.
    • switchtos

      protected List<Object> switchtos
      The monitors of blocked threads that need to be reactivated.
    • throwables

      protected Map<Object,Throwable> throwables
      The exceptions (if any) to be thrown in threads that need to be reactivated.
  • Constructor Details

    • Executor

      public Executor(IThreadPool threadpool)
      Create an executor object. Constructor for subclasses overriding the code() method.
    • Executor

      public Executor(IThreadPool threadpool, IExecutable executable)
      Create an executor object.
  • Method Details

    • run

      public void run()
      Execute the code.
      Specified by:
      run in interface Runnable
    • execute

      public boolean execute()
      Make sure a thread is executing the code.
    • shutdown

      public jadex.future.IFuture<Void> shutdown()
      Shutdown the executor.
    • setExecutable

      public void setExecutable(IExecutable executable)
      Set the executable.
      Parameters:
      executable - The executable.
    • code

      protected boolean code()
      The code to be run.
      Returns:
      True, when execution should continue.
    • isRunning

      protected boolean isRunning()
      Check if the executor is running. Should only be called, when access to executor is correctly synchronized, otherwise inconsistent values might be returned.
    • getThreadCount

      protected int getThreadCount()
      Get the number of threads (running blocked) for this executor.
    • switchThread

      public void switchThread(Object monitor, Throwable t)
      Cease execution of the current thread and switch to another thread waiting for the given monitor.
      Parameters:
      monitor - The monitor to be notified.
      t - The exception to be thrown on the unblocked thread (null for continuing normal execution).
    • blockThread

      public void blockThread(Object monitor)
      Adjust to execution of current thread to be blocked.
    • toString

      public String toString()
      String representation.
      Overrides:
      toString in class Object
    • isSwitching

      public boolean isSwitching()
      Check if the executor wants to switch threads.