|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectsk.baka.tools.concurrent.DelayedExecutor<K,R>
K - the key (ID) type. The key must comply contract to act as a key in Java Map - for example, Object.equals(java.lang.Object)
and Object.hashCode() have to be implemented correctly.R - the runnable types@ThreadSafe public final class DelayedExecutor<K,R extends Runnable>
Performs an asynchronous Runnable execution.
executor. General idea: we are trying to prevent multiple simultaneous runs of
Runnables for a single ID. We will do this by creating a QueueRunner, one for each ID, which will handle Runnables
exclusively for that ID, in a sequential fashion, thus delaying the execution of the Runnable. We need to store these
runnables somewhere to allow proper task scheduling. This
storage must be thread-safe and must support atomic locking+retrieving of these runners. The ILockingStorage storage has these properties.
We will use locks provided by this storage to guard scheduling of the tasks.
The runnables are free to throw an exception - exceptions are caught and logged.
| Constructor Summary | |
|---|---|
DelayedExecutor(ExecutorService executorService)
Creates a new executor and prepares it for executing the tasks. |
|
DelayedExecutor(int nThreads,
String threadName)
Creates a new executor and prepares it for executing the tasks. |
|
| Method Summary | |
|---|---|
void |
execute(K id,
R command)
Schedules given command for execution. |
List<R> |
getRunningOrScheduled()
Returns a list of all routes either running or scheduled for an immediate execution. |
int |
getRunningOrScheduledCount()
Returns a count of all routes either running or scheduled for an immediate execution. |
boolean |
interrupt()
Interrupts all currently running runnables. |
static ThreadFactory |
newDaemonFactory(String name)
Creates a new factory which creates daemon threads using the Executors.defaultThreadFactory(). |
void |
shutdown()
Shuts down the scheduler. |
void |
shutdownNow()
Shuts down the scheduler. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public DelayedExecutor(int nThreads,
String threadName)
nThreads - maximum number of threads the executor will use to run runnables.threadName - the threads names will contain this stringpublic DelayedExecutor(ExecutorService executorService)
executorService - use this executor to run runnables.| Method Detail |
|---|
public void execute(K id,
R command)
id - the ID of the command, must not be nullcommand - the command to execute. The command is delayed if there is already another command with the same ID running. Must not be null.public List<R> getRunningOrScheduled()
public boolean interrupt()
public int getRunningOrScheduledCount()
public void shutdown()
throws InterruptedException
InterruptedException - thrown if the waiting for runnables to finish was interrupted.public void shutdownNow()
public static ThreadFactory newDaemonFactory(String name)
Executors.defaultThreadFactory().
name - create threads marked with this name.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||