sk.baka.tools.concurrent
Class DelayedExecutor<K,R extends Runnable>

java.lang.Object
  extended by sk.baka.tools.concurrent.DelayedExecutor<K,R>
Type Parameters:
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>
extends Object

Performs an asynchronous Runnable execution.

A storage of runners scheduled for execution in the 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.

Author:
Martin Vysny

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

DelayedExecutor

public DelayedExecutor(int nThreads,
                       String threadName)
Creates a new executor and prepares it for executing the tasks.

Parameters:
nThreads - maximum number of threads the executor will use to run runnables.
threadName - the threads names will contain this string

DelayedExecutor

public DelayedExecutor(ExecutorService executorService)
Creates a new executor and prepares it for executing the tasks.

Parameters:
executorService - use this executor to run runnables.
Method Detail

execute

public void execute(K id,
                    R command)
Schedules given command for execution. The execution is performed ASAP, as long as there is no runnable with given ID already running. Memory effects: Scheduling of a command happens-before the command is executed.

Parameters:
id - the ID of the command, must not be null
command - the command to execute. The command is delayed if there is already another command with the same ID running. Must not be null.

getRunningOrScheduled

public List<R> getRunningOrScheduled()
Returns a list of all routes either running or scheduled for an immediate execution.

Returns:
list of all running or scheduled runnables.

interrupt

public boolean interrupt()
Interrupts all currently running runnables. This does not terminate the executor nor clears its execution queue. Some tasks may even not be executed. Use with caution.

Returns:
true if at least one thread was interrupted, false otherwise.

getRunningOrScheduledCount

public int getRunningOrScheduledCount()
Returns a count of all routes either running or scheduled for an immediate execution.

Returns:
count of all running or scheduled runnables.

shutdown

public void shutdown()
              throws InterruptedException
Shuts down the scheduler. All worker threads are interrupted. The method terminates after all currently executing runnables has been terminated.

Throws:
InterruptedException - thrown if the waiting for runnables to finish was interrupted.

shutdownNow

public void shutdownNow()
Shuts down the scheduler. All worker threads are interrupted. The method terminates immediately.


newDaemonFactory

public static ThreadFactory newDaemonFactory(String name)
Creates a new factory which creates daemon threads using the Executors.defaultThreadFactory().

Parameters:
name - create threads marked with this name.
Returns:
a daemon thread factory.


Copyright © 2009-2010. All Rights Reserved.