org.multiverse.transactional.executors
Class TransactionalThreadPoolExecutor

java.lang.Object
  extended by java.util.concurrent.AbstractExecutorService
      extended by org.multiverse.transactional.executors.TransactionalThreadPoolExecutor
All Implemented Interfaces:
java.util.concurrent.Executor, java.util.concurrent.ExecutorService

public class TransactionalThreadPoolExecutor
extends java.util.concurrent.AbstractExecutorService

A transactional Executor implementation that looks a lot like the ThreadPoolExecutor.

Known concurrent limitations

  1. no concurrent takes and puts
  2. no concurrent takes (not really a big issue since the takes only have a very short transactions)
  3. no concurrent puts
This will eventually be solved when a better queue implementation is found.

todo:

  1. Shutdown now not implemented

Exception handling
If an Exception is thrown when a command is executed, it will not be logged and discarded instead. In the ThreadPoolExecutor it is possible to override the ThreadPoolExecutor.afterExecute(Runnable, Throwable) but this functionality isn't provided yet. So the best thing that you can do is wrap the command in a logging Runnable that catches and logs the exception.

A task executes by a TransactionalThreadPoolExecutor will not automatically receives its own transaction.

Author:
Peter Veentjer.

Constructor Summary
TransactionalThreadPoolExecutor()
          Creates a new TransactionalThreadPoolExecutor with a unbound workqueue and 1 as corePoolSize and maxPoolSize.
TransactionalThreadPoolExecutor(java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue)
          Creates a new TransactionalThreadPoolExecutor with the given workQueue and 1 as corePoolSize and maxPoolSize.
TransactionalThreadPoolExecutor(java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue, int corePoolSize)
          Creates a new TransactionalThreadPoolExecutor.
TransactionalThreadPoolExecutor(int poolSize)
          Creates a new TransactionalThreadPool with an unbound workqueue and the provided poolSize.
 
Method Summary
 void awaitTermination()
          Awaits for this TransactionalThreadPoolExecutor to complete.
 boolean awaitTermination(long timeout, java.util.concurrent.TimeUnit unit)
           
 void awaitTerminationUninterruptibly()
          Awaits for this TransactionalThreadPoolExecutor to complete without the possibility of being interrupted.
 void execute(java.lang.Runnable command)
           
 int getCorePoolSize()
          Returns the minimum number of threads in this TransactionalThreadPoolExecutor.
 int getCurrentPoolSize()
          Returns the current number of threads in this TransactionalThreadPoolExecutor.
 org.multiverse.transactional.executors.TransactionalThreadPoolExecutor.State getState()
          Returns the State this TransactionalThreadPoolExecutor has.
 java.util.concurrent.ThreadFactory getThreadFactory()
          Returns the thread factory used to create new threads.
 java.util.concurrent.BlockingQueue<java.lang.Runnable> getWorkQueue()
          Returns the BlockingQueue this TransactionalThreadPoolExecutor uses to store unprocessed work.
 boolean isShutdown()
           
 boolean isStarted()
           
 boolean isTerminated()
           
 void setCorePoolSize(int newCorePoolSize)
          Sets the corePoolSize of this TransactionalThreadPoolExecutor.
 void setThreadFactory(java.util.concurrent.ThreadFactory threadFactory)
          Sets the thread factory used to create new threads.
 void shutdown()
           
 java.util.List<java.lang.Runnable> shutdownNow()
           
 void start()
          Starts this TransactionalThreadPoolExecutor.
 
Methods inherited from class java.util.concurrent.AbstractExecutorService
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submit
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TransactionalThreadPoolExecutor

public TransactionalThreadPoolExecutor()
Creates a new TransactionalThreadPoolExecutor with a unbound workqueue and 1 as corePoolSize and maxPoolSize.

If no limit is placed on the workQueue is could lead to a system not degrading gracefull because it runs out of memory instead of rejecting tasks.


TransactionalThreadPoolExecutor

public TransactionalThreadPoolExecutor(int poolSize)
Creates a new TransactionalThreadPool with an unbound workqueue and the provided poolSize.

Parameters:
poolSize - the maximum and core poolsize.
Throws:
java.lang.IllegalArgumentException - if poolSize smaller than 0.

TransactionalThreadPoolExecutor

public TransactionalThreadPoolExecutor(java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue)
Creates a new TransactionalThreadPoolExecutor with the given workQueue and 1 as corePoolSize and maxPoolSize.

Parameters:
workQueue - the BlockingQueue to store unprocessed work.
Throws:
java.lang.NullPointerException - if workQueue is null.

TransactionalThreadPoolExecutor

public TransactionalThreadPoolExecutor(java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue,
                                       int corePoolSize)
Creates a new TransactionalThreadPoolExecutor.

Parameters:
workQueue - the BlockingQueue to store unprocessed work.
corePoolSize - the minimum number of threads in this TransactionalThreadPoolExecutor.
Throws:
java.lang.NullPointerException - if workQueue is null.
java.lang.IllegalArgumentException - if corePoolSize is smaller than zero or if maxPoolSize smaller than corePoolSize.
Method Detail

getCorePoolSize

public int getCorePoolSize()
Returns the minimum number of threads in this TransactionalThreadPoolExecutor. The returned value will always be equal or larger than zero.

Returns:
the minimum number of threads in this TransactionalThreadPoolExecutor.

setCorePoolSize

public void setCorePoolSize(int newCorePoolSize)
Sets the corePoolSize of this TransactionalThreadPoolExecutor. If the TransactionalThreadPoolExecutor is unstarted, it isn't started. If the newCorePoolSize is larger than the maximumPoolSize, it automatically sets the maxPoolSize to newCorePoolSize.

Parameters:
newCorePoolSize - the new core poolsize.
Throws:
java.lang.IllegalArgumentException - if newCorePoolSize is smaller than 0.
java.lang.IllegalStateException - if this TransactionalThreadPoolExecutor is shutdown or terminated.

getCurrentPoolSize

public int getCurrentPoolSize()
Returns the current number of threads in this TransactionalThreadPoolExecutor.

Returns:
the current number of threads.

getWorkQueue

public java.util.concurrent.BlockingQueue<java.lang.Runnable> getWorkQueue()
Returns the BlockingQueue this TransactionalThreadPoolExecutor uses to store unprocessed work.

Returns:
the BlockingQueue used to store unprocessed work.

getState

public org.multiverse.transactional.executors.TransactionalThreadPoolExecutor.State getState()
Returns the State this TransactionalThreadPoolExecutor has.

Returns:
the State this TransactionalThreadPoolExecutor has.

setThreadFactory

public void setThreadFactory(java.util.concurrent.ThreadFactory threadFactory)
Sets the thread factory used to create new threads.

Parameters:
threadFactory - the new thread factory
Throws:
java.lang.NullPointerException - if threadFactory is null
See Also:
getThreadFactory()

getThreadFactory

public java.util.concurrent.ThreadFactory getThreadFactory()
Returns the thread factory used to create new threads.

Returns:
the current thread factory
See Also:
setThreadFactory(java.util.concurrent.ThreadFactory)

shutdown

public void shutdown()

shutdownNow

public java.util.List<java.lang.Runnable> shutdownNow()

isShutdown

public boolean isShutdown()

isTerminated

public boolean isTerminated()

isStarted

public boolean isStarted()

awaitTermination

public boolean awaitTermination(long timeout,
                                java.util.concurrent.TimeUnit unit)
                         throws java.lang.InterruptedException
Throws:
java.lang.InterruptedException

awaitTermination

public void awaitTermination()
                      throws java.lang.InterruptedException
Awaits for this TransactionalThreadPoolExecutor to complete. This call can be interrupted.

Throws:
java.lang.InterruptedException - if the Thread was interrupted.

awaitTerminationUninterruptibly

public void awaitTerminationUninterruptibly()
Awaits for this TransactionalThreadPoolExecutor to complete without the possibility of being interrupted.


execute

public void execute(java.lang.Runnable command)

start

public void start()
Starts this TransactionalThreadPoolExecutor. If the TransactionalThreadPoolExecutor already is started, the call is ignored.

Throws:
java.lang.IllegalStateException - if the this TransactionalThreadPoolExecutor already is shutdown or terminated.


Copyright © 2008-2010 Multiverse. All Rights Reserved.