Package jadex.commons.concurrent
Class Executor
- java.lang.Object
-
- jadex.commons.concurrent.Executor
-
- All Implemented Interfaces:
java.lang.Runnable
public class Executor extends java.lang.Object implements java.lang.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 Summary
Fields Modifier and Type Field Description protected IExecutable
executable
The executable.static java.lang.ThreadLocal<Executor>
EXECUTOR
The executor belonging to a thread.protected int
exethreadcnt
The number of current threads for this executor.protected java.lang.Object
monitor
The monitor to synchronize with at thread start (if any).protected boolean
running
Flag indicating if the thread is running.protected java.util.List<Future<java.lang.Void>>
shutdownfutures
The shutdown futures.protected java.util.List<java.lang.Object>
switchtos
The monitors of blocked threads that need to be reactivated.protected IThreadPool
threadpool
The thread pool.protected java.util.Map<java.lang.Object,java.lang.Throwable>
throwables
The exceptions (if any) to be thrown in threads that need to be reactivated.protected boolean
wanttorun
Flag indicating if the thread wants to run.
-
Constructor Summary
Constructors Constructor Description Executor(IThreadPool threadpool)
Create an executor object.Executor(IThreadPool threadpool, IExecutable executable)
Create an executor object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
blockThread(java.lang.Object monitor)
Adjust to execution of current thread to be blocked.protected boolean
code()
The code to be run.boolean
execute()
Make sure a thread is executing the code.protected int
getThreadCount()
Get the number of threads (running blocked) for this executor.protected boolean
isRunning()
Check if the executor is running.boolean
isSwitching()
Check if the executor wants to switch threads.void
run()
Execute the code.void
setExecutable(IExecutable executable)
Set the executable.IFuture<java.lang.Void>
shutdown()
Shutdown the executor.void
switchThread(java.lang.Object monitor, java.lang.Throwable t)
Cease execution of the current thread and switch to another thread waiting for the given monitor.java.lang.String
toString()
String representation.
-
-
-
Field Detail
-
EXECUTOR
public static final java.lang.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 java.util.List<Future<java.lang.Void>> shutdownfutures
The shutdown futures.
-
monitor
protected java.lang.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 java.util.List<java.lang.Object> switchtos
The monitors of blocked threads that need to be reactivated.
-
throwables
protected java.util.Map<java.lang.Object,java.lang.Throwable> throwables
The exceptions (if any) to be thrown in threads that need to be reactivated.
-
-
Constructor Detail
-
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 Detail
-
run
public void run()
Execute the code.- Specified by:
run
in interfacejava.lang.Runnable
-
execute
public boolean execute()
Make sure a thread is executing the code.
-
shutdown
public IFuture<java.lang.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(java.lang.Object monitor, java.lang.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(java.lang.Object monitor)
Adjust to execution of current thread to be blocked.
-
toString
public java.lang.String toString()
String representation.- Overrides:
toString
in classjava.lang.Object
-
isSwitching
public boolean isSwitching()
Check if the executor wants to switch threads.
-
-