net.sf.asyncobjects.vats
Class ExecutorRunner

java.lang.Object
  extended by net.sf.asyncobjects.vats.VatRunner
      extended by net.sf.asyncobjects.vats.ExecutorRunner
All Implemented Interfaces:
VatFactory

public class ExecutorRunner
extends VatRunner

This runner is being run over some Executor service. Note that it requres executor to execute tasks asynchronously. The blocking or direct execution when calling Executor.execute(Runnable) will cause deadlocks.

Author:
const

Nested Class Summary
 
Nested classes/interfaces inherited from class net.sf.asyncobjects.vats.VatRunner
VatRunner.VatRunnerState
 
Field Summary
 
Fields inherited from class net.sf.asyncobjects.vats.VatRunner
state, stateLock
 
Constructor Summary
ExecutorRunner(Executor executor)
          A constructor of executor
 
Method Summary
static ThreadFactory getThreadFactory(ThreadFactory factory, String namePrefix, Boolean isDaemon)
          Get thread factory
protected  boolean hasEvents(Vat vat, Object ticket)
          This method is called by the vat when new events have arrived.
static ExecutorRunner newCachedRunner()
          Creates new runner over executor created by Executors.newCachedThreadPool().
static ExecutorRunner newCachedRunner(String namePrefix)
          Creates new runner over executor created by Executors.newCachedThreadPool(ThreadFactory).
static ExecutorRunner newCachedRunner(String namePrefix, Boolean isDaemon)
          Creates new runner over executor created by Executors.newCachedThreadPool(ThreadFactory).
static ExecutorRunner newCachedRunner(ThreadFactory threadFactory)
          Creates new runner over executor created by Executors.newCachedThreadPool(ThreadFactory).
static ExecutorRunner newCachedRunner(ThreadFactory threadFactory, String namePrefix, Boolean isDaemon)
          Creates new runner over executor created by Executors.newCachedThreadPool(ThreadFactory).
static ExecutorRunner newFixedThreadPoolRunner(int nThreads)
          Creates new runner over fixed size thread pool
static ExecutorRunner newFixedThreadPoolRunner(int nThreads, Boolean isDaemon)
          Creates new runner over fixed size thread pool
static ExecutorRunner newFixedThreadPoolRunner(int nThreads, String namePrefix)
          Creates new runner over fixed size thread pool
static ExecutorRunner newFixedThreadPoolRunner(int nThreads, String namePrefix, Boolean isDaemon)
          Creates new runner over fixed size thread pool
static ExecutorRunner newFixedThreadPoolRunner(int nThreads, ThreadFactory threadFactory, String namePrefix, Boolean isDaemon)
          Creates new runner over fixed size thread pool
 
Methods inherited from class net.sf.asyncobjects.vats.VatRunner
cancelledEqueuedVat, getBachingFactor, getState, newVat, newVat, setBachingFactor, setStarted, setStarting, setState, setStopped, setStopping
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExecutorRunner

public ExecutorRunner(Executor executor)
A constructor of executor

Parameters:
executor - an exector to use
Method Detail

hasEvents

protected boolean hasEvents(Vat vat,
                            Object ticket)
Description copied from class: VatRunner
This method is called by the vat when new events have arrived. This method should never block and to be as fast as possible. This method executed within synchronoization over Vat's state lock. The vat enqueues itself only if is not running or enqueued yet.

Specified by:
hasEvents in class VatRunner
Parameters:
vat - a vat that has new events and is associated with the runner
ticket - a ticket from the vat
Returns:
true if runner accpets the vat, false otherwise (happens if runner is stopped or is stopping)
See Also:
VatRunner.hasEvents(net.sf.asyncobjects.vats.Vat, java.lang.Object)

newCachedRunner

public static ExecutorRunner newCachedRunner(ThreadFactory threadFactory)
Creates new runner over executor created by Executors.newCachedThreadPool(ThreadFactory). Note that batching factor for this runner is Integer.MAX_VALUE because using other values will have almost the same effect but with lesser efficiency due to unnecessary context setting.

Parameters:
threadFactory - a thread factory
Returns:
an runner

newCachedRunner

public static ExecutorRunner newCachedRunner()
Creates new runner over executor created by Executors.newCachedThreadPool().

Returns:
an runner

newCachedRunner

public static ExecutorRunner newCachedRunner(ThreadFactory threadFactory,
                                             String namePrefix,
                                             Boolean isDaemon)
Creates new runner over executor created by Executors.newCachedThreadPool(ThreadFactory).

Parameters:
threadFactory - thread factory
namePrefix - name prefix. if null prefix is ignored
isDaemon - if thread should be created as daemon threads
Returns:
an runner

newCachedRunner

public static ExecutorRunner newCachedRunner(String namePrefix,
                                             Boolean isDaemon)
Creates new runner over executor created by Executors.newCachedThreadPool(ThreadFactory).

Parameters:
namePrefix - name prefix. if null prefix is ignored
isDaemon - if thread should be created as daemon threads
Returns:
an runner

newCachedRunner

public static ExecutorRunner newCachedRunner(String namePrefix)
Creates new runner over executor created by Executors.newCachedThreadPool(ThreadFactory).

Parameters:
namePrefix - name prefix. if null prefix is ignored
Returns:
an runner

newFixedThreadPoolRunner

public static ExecutorRunner newFixedThreadPoolRunner(int nThreads,
                                                      ThreadFactory threadFactory,
                                                      String namePrefix,
                                                      Boolean isDaemon)
Creates new runner over fixed size thread pool

Parameters:
nThreads - number of threads
threadFactory - thread factory
namePrefix - name prefix. if null prefix is ignored
isDaemon - if thread should be created as daemon threads
Returns:
a new runner

newFixedThreadPoolRunner

public static ExecutorRunner newFixedThreadPoolRunner(int nThreads,
                                                      String namePrefix,
                                                      Boolean isDaemon)
Creates new runner over fixed size thread pool

Parameters:
nThreads - number of threads
namePrefix - name prefix. if null prefix is ignored
isDaemon - if thread should be created as daemon threads
Returns:
a new runner

newFixedThreadPoolRunner

public static ExecutorRunner newFixedThreadPoolRunner(int nThreads,
                                                      Boolean isDaemon)
Creates new runner over fixed size thread pool

Parameters:
nThreads - number of threads
isDaemon - if thread should be created as daemon threads
Returns:
a new runner

newFixedThreadPoolRunner

public static ExecutorRunner newFixedThreadPoolRunner(int nThreads,
                                                      String namePrefix)
Creates new runner over fixed size thread pool

Parameters:
nThreads - number of threads
namePrefix - name prefix. if null prefix is ignored
Returns:
a new runner

newFixedThreadPoolRunner

public static ExecutorRunner newFixedThreadPoolRunner(int nThreads)
Creates new runner over fixed size thread pool

Parameters:
nThreads - number of threads
Returns:
a new runner

getThreadFactory

public static ThreadFactory getThreadFactory(ThreadFactory factory,
                                             String namePrefix,
                                             Boolean isDaemon)
Get thread factory

Parameters:
factory - a factory
namePrefix - a name prefix for new threads
isDaemon - a value of isDaemon parameters
Returns:
warper thread factory that sets parameters


Copyright © 2002-2007 Constantine Plotnikov. All Rights Reserved.