org.biojava3.core.util
Class ConcurrencyTools

java.lang.Object
  extended by org.biojava3.core.util.ConcurrencyTools

public class ConcurrencyTools
extends Object

Static utility to easily share a thread pool for concurrent/parallel/lazy execution. To exit cleanly, shutdown() or shutdownAndAwaitTermination() must be called after all tasks have been submitted.

Author:
Mark Chapman

Method Summary
static ThreadPoolExecutor getThreadPool()
          Returns current shared thread pool.
static void setThreadPool(ThreadPoolExecutor pool)
          Sets thread pool to any given ThreadPoolExecutor to allow use of an alternative execution style.
static void setThreadPoolCPUsAvailable(int cpus)
          Sets thread pool to reserve a given number of processor cores for foreground or other use.
static void setThreadPoolCPUsFraction(float fraction)
          Sets thread pool to a given fraction of the available processors.
static void setThreadPoolDefault()
          Sets thread pool to default of 1 background thread for each processor core.
static void setThreadPoolSingle()
          Sets thread pool to a single background thread.
static void setThreadPoolSize(int threads)
          Sets thread pool to given size.
static void shutdown()
          Disables new tasks from being submitted and closes the thread pool cleanly.
static void shutdownAndAwaitTermination()
          Closes the thread pool.
static
<T> Future<T>
submit(Callable<T> task)
          Queues up a task and adds a default log entry.
static
<T> Future<T>
submit(Callable<T> task, String message)
          Queues up a task and adds a log entry.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getThreadPool

public static ThreadPoolExecutor getThreadPool()
Returns current shared thread pool. Starts up a new pool, if necessary.

Returns:
shared thread pool

setThreadPoolCPUsAvailable

public static void setThreadPoolCPUsAvailable(int cpus)
Sets thread pool to reserve a given number of processor cores for foreground or other use.

Parameters:
cpus - number of processor cores to reserve

setThreadPoolCPUsFraction

public static void setThreadPoolCPUsFraction(float fraction)
Sets thread pool to a given fraction of the available processors.

Parameters:
fraction - portion of available processors to use in thread pool

setThreadPoolDefault

public static void setThreadPoolDefault()
Sets thread pool to default of 1 background thread for each processor core.


setThreadPoolSingle

public static void setThreadPoolSingle()
Sets thread pool to a single background thread.


setThreadPoolSize

public static void setThreadPoolSize(int threads)
Sets thread pool to given size.

Parameters:
threads - number of threads in pool

setThreadPool

public static void setThreadPool(ThreadPoolExecutor pool)
Sets thread pool to any given ThreadPoolExecutor to allow use of an alternative execution style.

Parameters:
pool - thread pool to share

shutdown

public static void shutdown()
Disables new tasks from being submitted and closes the thread pool cleanly.


shutdownAndAwaitTermination

public static void shutdownAndAwaitTermination()
Closes the thread pool. Waits 1 minute for a clean exit; if necessary, waits another minute for cancellation.


submit

public static <T> Future<T> submit(Callable<T> task,
                                   String message)
Queues up a task and adds a log entry.

Type Parameters:
T - type returned from the submitted task
Parameters:
task - submitted task
message - logged message
Returns:
future on which the desired value is retrieved by calling get()

submit

public static <T> Future<T> submit(Callable<T> task)
Queues up a task and adds a default log entry.

Type Parameters:
T - type returned from the submitted task
Parameters:
task - submitted task
Returns:
future on which the desired value is retrieved by calling get()