Package org.jppf.utils.concurrent
Class ConcurrentUtils
java.lang.Object
org.jppf.utils.concurrent.ConcurrentUtils
A set of utility methods to facilitate concurrent and multithreaded programming.
- Since:
- 5.0
- Author:
- Laurent Cohen
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceThis interface represents a condition to evaluate to eithertrueorfalse.static interfaceThis interface handles exceptions raised by itsevaluate()method and returnsfalsewhen it happens. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanawaitCondition(long millis, long sleepInterval, boolean throwExceptionOnTImeout, ConcurrentUtils.Condition condition) Wait until the specified condition is fulfilled, or the timeout expires, whichever happens first.static booleanawaitCondition(ConcurrentUtils.Condition condition, long millis, long sleepInterval, boolean throwExceptionOnTImeout) Wait until the specified condition is fulfilled, or the timeout expires, whichever happens first.static booleanawaitCondition(ThreadSynchronization monitor, ConcurrentUtils.Condition condition, long millis, long sleepInterval) Wait until the specified condition is fulfilled, or the timeout expires, whichever happens first.static ThreadPoolExecutornewBoundedQueueExecutor(int coreThreads, int queueSize, long ttl, String threadNamePrefix) Create a pool that directly hands off new tasks, creating new threads as required.static ThreadPoolExecutornewDirectHandoffExecutor(int coreThreads, int maxThreads, long ttl, String threadNamePrefix) Create a pool that directly hands off new tasks, creating new threads as required.static ThreadPoolExecutornewDirectHandoffExecutor(int coreThreads, long ttl, String threadNamePrefix) Create a pool that directly hands off new tasks, creating new threads as required.static ThreadPoolExecutornewFixedExecutor(int coreThreads, long ttl, String threadNamePrefix) Create a fixed-size pool.static ThreadPoolExecutornewFixedExecutor(int coreThreads, String threadNamePrefix) Create a fixed-size pool.static ExecutorServicenewJPPFDirectHandoffExecutor(int coreThreads, int maxThreads, long ttl, String threadNamePrefix) Create a pool that directly hands off new tasks, creating new threads as required.static ExecutorServicenewJPPFFixedThreadPool(int coreThreads, String threadNamePrefix) Create aJPPFThreadPool.static ExecutorServicenewJPPFThreadPool(int coreThreads, int maxThreads, long ttl, String threadNamePrefix) Create aJPPFThreadPool.static voidwaitForMonitor(Object monitor, ConcurrentUtils.Condition condition, long timeout, long waitTime, boolean throwExceptionOnTImeout) Wait on the specified monitor for at most the specified tiemout or until the specified condition is fulfilled, waiting by increment of the specified wait time.
-
Method Details
-
awaitCondition
public static boolean awaitCondition(ThreadSynchronization monitor, ConcurrentUtils.Condition condition, long millis, long sleepInterval) throws IllegalArgumentException Wait until the specified condition is fulfilled, or the timeout expires, whichever happens first. The specified monitor may be notified at any time during the execution of this method, at which time it will check the condition again.- Parameters:
monitor- the monitor to wait for.condition- the condition to check.millis- the milliseconds part of the timeout. A value of zero means an infinite timeout.sleepInterval- how long to wait between evaluations of the condition, in millis.- Returns:
- true if the condition is
nullor was fulfilled before the timeout expired,falseotherwise. - Throws:
IllegalArgumentException- if the millis or nanos are negative, or if the nanos are greater than 999999.
-
awaitCondition
public static boolean awaitCondition(ConcurrentUtils.Condition condition, long millis, long sleepInterval, boolean throwExceptionOnTImeout) throws IllegalArgumentException, JPPFTimeoutException Wait until the specified condition is fulfilled, or the timeout expires, whichever happens first. The specified monitor may be notified at any time during the execution of this method, at which time it will check the condition again.- Parameters:
condition- the condition to check.millis- the milliseconds part of the timeout. A value of zero means an infinite timeout.sleepInterval- how long to wait between evaluations of the condition, in millis.throwExceptionOnTImeout- whether to raise an exception if the timeout expires.- Returns:
- true if the condition is
nullor was fulfilled before the timeout expired,falseotherwise. - Throws:
IllegalArgumentException- if the millis or nanos are negative, or if the nanos are greater than 999999.JPPFTimeoutException- if the timeout expires andthrowExceptionOnTImeoutistrue.
-
awaitCondition
public static boolean awaitCondition(long millis, long sleepInterval, boolean throwExceptionOnTImeout, ConcurrentUtils.Condition condition) throws IllegalArgumentException, JPPFTimeoutException Wait until the specified condition is fulfilled, or the timeout expires, whichever happens first. The specified monitor may be notified at any time during the execution of this method, at which time it will check the condition again.- Parameters:
condition- the condition to check.millis- the milliseconds part of the timeout. A value of zero means an infinite timeout.sleepInterval- how long to wait between evaluations of the condition, in millis.throwExceptionOnTImeout- whether to raise an exception if the timeout expires.- Returns:
- true if the condition is
nullor was fulfilled before the timeout expired,falseotherwise. - Throws:
IllegalArgumentException- if the millis or nanos are negative, or if the nanos are greater than 999999.JPPFTimeoutException- if the timeout expires andthrowExceptionOnTImeoutistrue.
-
waitForMonitor
public static void waitForMonitor(Object monitor, ConcurrentUtils.Condition condition, long timeout, long waitTime, boolean throwExceptionOnTImeout) throws Exception Wait on the specified monitor for at most the specified tiemout or until the specified condition is fulfilled, waiting by increment of the specified wait time.- Parameters:
monitor- the monitor to wait on.condition- the condition to check.timeout- the wait timeout.waitTime- the max wait time in the wait loop.throwExceptionOnTImeout- whether to raise an exception if the timeout expires.- Throws:
Exception- if any error occurs.
-
newDirectHandoffExecutor
public static ThreadPoolExecutor newDirectHandoffExecutor(int coreThreads, long ttl, String threadNamePrefix) Create a pool that directly hands off new tasks, creating new threads as required.- Parameters:
coreThreads- the number of core threads.ttl- the threads' time-to -live in millis.threadNamePrefix- name prefix for created threads.- Returns:
- a new
ThreadPoolExecutorinstance.
-
newDirectHandoffExecutor
public static ThreadPoolExecutor newDirectHandoffExecutor(int coreThreads, int maxThreads, long ttl, String threadNamePrefix) Create a pool that directly hands off new tasks, creating new threads as required.- Parameters:
coreThreads- the number of core threads.maxThreads- the maximum number of threads.ttl- the threads' time-to -live in millis.threadNamePrefix- name prefix for created threads.- Returns:
- a new
ThreadPoolExecutorinstance.
-
newBoundedQueueExecutor
public static ThreadPoolExecutor newBoundedQueueExecutor(int coreThreads, int queueSize, long ttl, String threadNamePrefix) Create a pool that directly hands off new tasks, creating new threads as required.- Parameters:
coreThreads- the number of core threads.queueSize- the size of the bounded queue.ttl- the threads' time-to -live in millis.threadNamePrefix- name prefix for created threads.- Returns:
- a new
ThreadPoolExecutorinstance.
-
newFixedExecutor
Create a fixed-size pool.- Parameters:
coreThreads- the number of core threads.threadNamePrefix- name prefix for created threads.- Returns:
- a new
ThreadPoolExecutorinstance.
-
newFixedExecutor
public static ThreadPoolExecutor newFixedExecutor(int coreThreads, long ttl, String threadNamePrefix) Create a fixed-size pool.- Parameters:
coreThreads- the number of core threads.ttl- the threads' time-to -live in millis.threadNamePrefix- name prefix for created threads.- Returns:
- a new
ThreadPoolExecutorinstance.
-
newJPPFFixedThreadPool
Create aJPPFThreadPool.- Parameters:
coreThreads- the number of core threads.threadNamePrefix- name prefix for created threads.- Returns:
- a new
JPPFThreadPoolinstance.
-
newJPPFThreadPool
public static ExecutorService newJPPFThreadPool(int coreThreads, int maxThreads, long ttl, String threadNamePrefix) Create aJPPFThreadPool.- Parameters:
coreThreads- the number of core threads.maxThreads- the maximum number of threads.ttl- the non-core threads' time-to-live in millis.threadNamePrefix- name prefix for created threads.- Returns:
- a new
JPPFThreadPoolinstance.
-
newJPPFDirectHandoffExecutor
public static ExecutorService newJPPFDirectHandoffExecutor(int coreThreads, int maxThreads, long ttl, String threadNamePrefix) Create a pool that directly hands off new tasks, creating new threads as required.- Parameters:
coreThreads- the number of core threads.maxThreads- the maximum number of threads.ttl- the threads' time-to -live in millis.threadNamePrefix- name prefix for created threads.- Returns:
- a new
ThreadPoolExecutorinstance.
-