Class ConcurrentUtils

java.lang.Object
org.jppf.utils.concurrent.ConcurrentUtils

public final class ConcurrentUtils extends Object
A set of utility methods to facilitate concurrent and multithreaded programming.
Since:
5.0
Author:
Laurent Cohen
  • 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 null or was fulfilled before the timeout expired, false otherwise.
      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 null or was fulfilled before the timeout expired, false otherwise.
      Throws:
      IllegalArgumentException - if the millis or nanos are negative, or if the nanos are greater than 999999.
      JPPFTimeoutException - if the timeout expires and throwExceptionOnTImeout is true.
    • 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 null or was fulfilled before the timeout expired, false otherwise.
      Throws:
      IllegalArgumentException - if the millis or nanos are negative, or if the nanos are greater than 999999.
      JPPFTimeoutException - if the timeout expires and throwExceptionOnTImeout is true.
    • 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 ThreadPoolExecutor instance.
    • 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 ThreadPoolExecutor instance.
    • 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 ThreadPoolExecutor instance.
    • newFixedExecutor

      public static ThreadPoolExecutor newFixedExecutor(int coreThreads, String threadNamePrefix)
      Create a fixed-size pool.
      Parameters:
      coreThreads - the number of core threads.
      threadNamePrefix - name prefix for created threads.
      Returns:
      a new ThreadPoolExecutor instance.
    • 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 ThreadPoolExecutor instance.
    • newJPPFFixedThreadPool

      public static ExecutorService newJPPFFixedThreadPool(int coreThreads, String threadNamePrefix)
      Create a JPPFThreadPool.
      Parameters:
      coreThreads - the number of core threads.
      threadNamePrefix - name prefix for created threads.
      Returns:
      a new JPPFThreadPool instance.
    • newJPPFThreadPool

      public static ExecutorService newJPPFThreadPool(int coreThreads, int maxThreads, long ttl, String threadNamePrefix)
      Create a JPPFThreadPool.
      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 JPPFThreadPool instance.
    • 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 ThreadPoolExecutor instance.