Package jadex.concurrent
Class Executor
java.lang.Object
jadex.concurrent.Executor
- All Implemented Interfaces:
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
FieldsModifier and TypeFieldDescriptionprotected IExecutable
The executable.static final ThreadLocal
<Executor> The executor belonging to a thread.protected int
The number of current threads for this executor.protected Object
The monitor to synchronize with at thread start (if any).protected boolean
Flag indicating if the thread is running.The shutdown futures.The monitors of blocked threads that need to be reactivated.protected IThreadPool
The thread pool.The exceptions (if any) to be thrown in threads that need to be reactivated.protected boolean
Flag indicating if the thread wants to run. -
Constructor Summary
ConstructorsConstructorDescriptionExecutor
(IThreadPool threadpool) Create an executor object.Executor
(IThreadPool threadpool, IExecutable executable) Create an executor object. -
Method Summary
Modifier and TypeMethodDescriptionvoid
blockThread
(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
Get the number of threads (running blocked) for this executor.protected boolean
Check if the executor is running.boolean
Check if the executor wants to switch threads.void
run()
Execute the code.void
setExecutable
(IExecutable executable) Set the executable.jadex.future.IFuture
<Void> shutdown()
Shutdown the executor.void
switchThread
(Object monitor, Throwable t) Cease execution of the current thread and switch to another thread waiting for the given monitor.toString()
String representation.
-
Field Details
-
EXECUTOR
The executor belonging to a thread. -
running
protected boolean runningFlag indicating if the thread is running. -
wanttorun
protected boolean wanttorunFlag indicating if the thread wants to run. -
threadpool
The thread pool. -
executable
The executable. -
shutdownfutures
The shutdown futures. -
monitor
The monitor to synchronize with at thread start (if any). -
exethreadcnt
protected int exethreadcntThe number of current threads for this executor. -
switchtos
The monitors of blocked threads that need to be reactivated. -
throwables
The exceptions (if any) to be thrown in threads that need to be reactivated.
-
-
Constructor Details
-
Executor
Create an executor object. Constructor for subclasses overriding the code() method. -
Executor
Create an executor object.
-
-
Method Details
-
run
public void run()Execute the code. -
execute
public boolean execute()Make sure a thread is executing the code. -
shutdown
Shutdown the executor. -
setExecutable
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
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
Adjust to execution of current thread to be blocked. -
toString
String representation. -
isSwitching
public boolean isSwitching()Check if the executor wants to switch threads.
-