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.RunnableA 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 SummaryFields Modifier and Type Field Description protected IExecutableexecutableThe executable.static java.lang.ThreadLocal<Executor>EXECUTORThe executor belonging to a thread.protected intexethreadcntThe number of current threads for this executor.protected java.lang.ObjectmonitorThe monitor to synchronize with at thread start (if any).protected booleanrunningFlag indicating if the thread is running.protected java.util.List<Future<java.lang.Void>>shutdownfuturesThe shutdown futures.protected java.util.List<java.lang.Object>switchtosThe monitors of blocked threads that need to be reactivated.protected IThreadPoolthreadpoolThe thread pool.protected java.util.Map<java.lang.Object,java.lang.Throwable>throwablesThe exceptions (if any) to be thrown in threads that need to be reactivated.protected booleanwanttorunFlag indicating if the thread wants to run.
 - 
Constructor SummaryConstructors Constructor Description Executor(IThreadPool threadpool)Create an executor object.Executor(IThreadPool threadpool, IExecutable executable)Create an executor object.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description voidblockThread(java.lang.Object monitor)Adjust to execution of current thread to be blocked.protected booleancode()The code to be run.booleanexecute()Make sure a thread is executing the code.protected intgetThreadCount()Get the number of threads (running blocked) for this executor.protected booleanisRunning()Check if the executor is running.booleanisSwitching()Check if the executor wants to switch threads.voidrun()Execute the code.voidsetExecutable(IExecutable executable)Set the executable.IFuture<java.lang.Void>shutdown()Shutdown the executor.voidswitchThread(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.StringtoString()String representation.
 
- 
- 
- 
Field Detail- 
EXECUTORpublic static final java.lang.ThreadLocal<Executor> EXECUTOR The executor belonging to a thread.
 - 
runningprotected boolean running Flag indicating if the thread is running.
 - 
wanttorunprotected boolean wanttorun Flag indicating if the thread wants to run.
 - 
threadpoolprotected IThreadPool threadpool The thread pool.
 - 
executableprotected IExecutable executable The executable.
 - 
shutdownfuturesprotected java.util.List<Future<java.lang.Void>> shutdownfutures The shutdown futures.
 - 
monitorprotected java.lang.Object monitor The monitor to synchronize with at thread start (if any).
 - 
exethreadcntprotected int exethreadcnt The number of current threads for this executor.
 - 
switchtosprotected java.util.List<java.lang.Object> switchtos The monitors of blocked threads that need to be reactivated.
 - 
throwablesprotected 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- 
Executorpublic Executor(IThreadPool threadpool) Create an executor object. Constructor for subclasses overriding the code() method.
 - 
Executorpublic Executor(IThreadPool threadpool, IExecutable executable) Create an executor object.
 
- 
 - 
Method Detail- 
runpublic void run() Execute the code.- Specified by:
- runin interface- java.lang.Runnable
 
 - 
executepublic boolean execute() Make sure a thread is executing the code.
 - 
shutdownpublic IFuture<java.lang.Void> shutdown() Shutdown the executor.
 - 
setExecutablepublic void setExecutable(IExecutable executable) Set the executable.- Parameters:
- executable- The executable.
 
 - 
codeprotected boolean code() The code to be run.- Returns:
- True, when execution should continue.
 
 - 
isRunningprotected 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.
 - 
getThreadCountprotected int getThreadCount() Get the number of threads (running blocked) for this executor.
 - 
switchThreadpublic 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).
 
 - 
blockThreadpublic void blockThread(java.lang.Object monitor) Adjust to execution of current thread to be blocked.
 - 
toStringpublic java.lang.String toString() String representation.- Overrides:
- toStringin class- java.lang.Object
 
 - 
isSwitchingpublic boolean isSwitching() Check if the executor wants to switch threads.
 
- 
 
-