Package blbutil

Class MultiThreadUtils


  • public class MultiThreadUtils
    extends java.lang.Object
    Class Utilities contains miscellaneous static utility methods for multi-threaded programming.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void await​(java.util.concurrent.CountDownLatch latch)
      Blocks the current thread until the specified CountDownLatch has counted down to 0.
      static <E> void putInBlockingQ​(java.util.concurrent.BlockingQueue<E> q, E e)
      Inserts the specified element at the tail of the specified blocking queue, waiting for space to become available if the queue is full.
      static <E> boolean putInBlockingQ​(java.util.concurrent.BlockingQueue<E> q, E e, long timeout, java.util.concurrent.TimeUnit unit)
      Inserts the specified element at the tail of the specified blocking queue, waiting up to the specified time for space to become available if the queue is full.
      static void shutdownExecService​(java.util.concurrent.ExecutorService es)
      Shuts down and awaits termination of the specified ExecutorService.
      static <E> E takeFromBlockingQ​(java.util.concurrent.BlockingQueue<E> q)
      Removes and returns the element at the head of the specified blocking queue, waiting if necessary for an element to become available.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • putInBlockingQ

        public static <E> void putInBlockingQ​(java.util.concurrent.BlockingQueue<E> q,
                                              E e)
        Inserts the specified element at the tail of the specified blocking queue, waiting for space to become available if the queue is full. The Java Virtual Machine is terminated if an InterruptedException is thrown while waiting for space to be come available in the queue.
        Type Parameters:
        E - the element type
        Parameters:
        q - a blocking queue
        e - the element to add
        Throws:
        java.lang.NullPointerException - if q == null || e == null
      • putInBlockingQ

        public static <E> boolean putInBlockingQ​(java.util.concurrent.BlockingQueue<E> q,
                                                 E e,
                                                 long timeout,
                                                 java.util.concurrent.TimeUnit unit)
        Inserts the specified element at the tail of the specified blocking queue, waiting up to the specified time for space to become available if the queue is full. The Java Virtual Machine is terminated if an InterruptedException is thrown while waiting for space to be come available in the queue.
        Type Parameters:
        E - the element type
        Parameters:
        q - a blocking queue
        e - the element to add
        timeout - the number of time units to wait before giving up
        unit - the time unit
        Returns:
        true if element was added to the queue, and false otherwise
        Throws:
        java.lang.NullPointerException - if q == null || e == null || unit == null
      • takeFromBlockingQ

        public static <E> E takeFromBlockingQ​(java.util.concurrent.BlockingQueue<E> q)
        Removes and returns the element at the head of the specified blocking queue, waiting if necessary for an element to become available. The Java Virtual Machine is terminated if an InterruptedException is thrown while waiting for space to be come available in the queue.
        Type Parameters:
        E - the element type
        Parameters:
        q - a blocking queue
        Returns:
        the element at the head of the queue
      • await

        public static void await​(java.util.concurrent.CountDownLatch latch)
        Blocks the current thread until the specified CountDownLatch has counted down to 0. The Java Virtual Machine is terminated if an InterruptedException is thrown while waiting for for the CountDownLatch to count down to 0.
        Parameters:
        latch - the count down latch
        Throws:
        java.lang.NullPointerException - if latch == null
      • shutdownExecService

        public static void shutdownExecService​(java.util.concurrent.ExecutorService es)
        Shuts down and awaits termination of the specified ExecutorService. The Java Virtual Machine is terminated if an InterruptedException is thrown while awaiting termination of the executor service.
        Parameters:
        es - the executor service to be shut down
        Throws:
        java.lang.NullPointerException - if es == null