org.jclouds.concurrent
Class DynamicExecutors

java.lang.Object
  extended by org.jclouds.concurrent.DynamicExecutors

public class DynamicExecutors
extends Object

Factory and utility methods for handling DynamicThreadPoolExecutor.

Author:
kimchy (shay.banon)

Method Summary
static ThreadFactory daemonThreadFactory(String namePrefix)
          A priority based thread factory, for all Thread priority constants: Thread.MIN_PRIORITY, Thread.NORM_PRIORITY, Thread.MAX_PRIORITY;

This factory is used instead of Executers.DefaultThreadFactory to allow manipulation of priority and thread owner name.

static ExecutorService newBlockingThreadPool(int min, int max, long keepAliveTime, int capacity, long waitTime)
          Creates a thread pool similar to that constructed by newScalingThreadPool(int, int, long), but blocks the call to execute if the queue has reached it's capacity, and all max threads are busy handling requests.
static ExecutorService newBlockingThreadPool(int min, int max, long keepAliveTime, int capacity, long waitTime, ThreadFactory threadFactory)
          Creates a thread pool, same as in newBlockingThreadPool(int, int, long, int, long), using the provided ThreadFactory to create new threads when needed.
static ExecutorService newScalingThreadPool(int min, int max, long keepAliveTime)
          Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available.
static ExecutorService newScalingThreadPool(int min, int max, long keepAliveTime, ThreadFactory threadFactory)
          Creates a thread pool, same as in newScalingThreadPool(int, int, long), using the provided ThreadFactory to create new threads when needed.
static ThreadFactory priorityThreadFactory(int priority, String namePrefix)
          A priority based thread factory, for all Thread priority constants: Thread.MIN_PRIORITY, Thread.NORM_PRIORITY, Thread.MAX_PRIORITY;

This factory is used instead of Executers.DefaultThreadFactory to allow manipulation of priority and thread owner name.

 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

newScalingThreadPool

public static ExecutorService newScalingThreadPool(int min,
                                                   int max,
                                                   long keepAliveTime)
Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. No more than max threads will be created. Threads that have not been used for a keepAlive timeout are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources other than the min specified.

Parameters:
min - the number of threads to keep in the pool, even if they are idle.
max - the maximum number of threads to allow in the pool.
keepAliveTime - when the number of threads is greater than the min, this is the maximum time that excess idle threads will wait for new tasks before terminating (in milliseconds).
Returns:
the newly created thread pool

newScalingThreadPool

public static ExecutorService newScalingThreadPool(int min,
                                                   int max,
                                                   long keepAliveTime,
                                                   ThreadFactory threadFactory)
Creates a thread pool, same as in newScalingThreadPool(int, int, long), using the provided ThreadFactory to create new threads when needed.

Parameters:
min - the number of threads to keep in the pool, even if they are idle.
max - the maximum number of threads to allow in the pool.
keepAliveTime - when the number of threads is greater than the min, this is the maximum time that excess idle threads will wait for new tasks before terminating (in milliseconds).
threadFactory - the factory to use when creating new threads.
Returns:
the newly created thread pool

newBlockingThreadPool

public static ExecutorService newBlockingThreadPool(int min,
                                                    int max,
                                                    long keepAliveTime,
                                                    int capacity,
                                                    long waitTime)
Creates a thread pool similar to that constructed by newScalingThreadPool(int, int, long), but blocks the call to execute if the queue has reached it's capacity, and all max threads are busy handling requests.

If the wait time of this queue has elapsed, a RejectedExecutionException will be thrown.

Parameters:
min - the number of threads to keep in the pool, even if they are idle.
max - the maximum number of threads to allow in the pool.
keepAliveTime - when the number of threads is greater than the min, this is the maximum time that excess idle threads will wait for new tasks before terminating (in milliseconds).
capacity - the fixed capacity of the underlying queue (resembles backlog).
waitTime - the wait time (in milliseconds) for space to become available in the queue.
Returns:
the newly created thread pool

newBlockingThreadPool

public static ExecutorService newBlockingThreadPool(int min,
                                                    int max,
                                                    long keepAliveTime,
                                                    int capacity,
                                                    long waitTime,
                                                    ThreadFactory threadFactory)
Creates a thread pool, same as in newBlockingThreadPool(int, int, long, int, long), using the provided ThreadFactory to create new threads when needed.

Parameters:
min - the number of threads to keep in the pool, even if they are idle.
max - the maximum number of threads to allow in the pool.
keepAliveTime - when the number of threads is greater than the min, this is the maximum time that excess idle threads will wait for new tasks before terminating (in milliseconds).
capacity - the fixed capacity of the underlying queue (resembles backlog).
waitTime - the wait time (in milliseconds) for space to become available in the queue.
threadFactory - the factory to use when creating new threads.
Returns:
the newly created thread pool

daemonThreadFactory

public static ThreadFactory daemonThreadFactory(String namePrefix)
A priority based thread factory, for all Thread priority constants: Thread.MIN_PRIORITY, Thread.NORM_PRIORITY, Thread.MAX_PRIORITY;

This factory is used instead of Executers.DefaultThreadFactory to allow manipulation of priority and thread owner name.

Parameters:
namePrefix - a name prefix for this thread
Returns:
a thread factory based on given priority.

priorityThreadFactory

public static ThreadFactory priorityThreadFactory(int priority,
                                                  String namePrefix)
A priority based thread factory, for all Thread priority constants: Thread.MIN_PRIORITY, Thread.NORM_PRIORITY, Thread.MAX_PRIORITY;

This factory is used instead of Executers.DefaultThreadFactory to allow manipulation of priority and thread owner name.

Parameters:
priority - The priority to be assigned to each thread; can be either Thread.MIN_PRIORITY, Thread.NORM_PRIORITY or Thread.MAX_PRIORITY.
namePrefix - a name prefix for this thread
Returns:
a thread factory based on given priority.


Copyright © 2009-2012 jclouds. All Rights Reserved.