Class Blocking


  • public class Blocking
    extends Object
    Blocking provides a set of helper methods related to blocking a thread.
    Author:
    mf 2015.02.24
    • Constructor Summary

      Constructors 
      Constructor Description
      Blocking()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void await​(Condition cond)
      Await for the Condition to be signaled while still respecting the calling thread's timeout.
      static void await​(Condition cond, long cNanos)
      Await for the Condition to be signaled while still respecting the calling thread's timeout.
      static void await​(Condition cond, long time, TimeUnit unit)
      Await for the Condition to be signaled while still respecting the calling thread's timeout.
      static void connect​(Socket socket, SocketAddress addr)
      Connect a socket while still respecting the calling thread's timeout.
      static void connect​(Socket socket, SocketAddress addr, int cMillis)
      Connect a socket within a given timeout while still respecting the calling thread's timeout.
      static boolean interrupted()
      Return true if the thread is interrupted or timed out.
      static void lockInterruptibly​(Lock lock)
      Acquire a lock while still respecting the calling thread's timeout.
      static void park​(Object oBlocker)
      Invoke LockSupport.park() while still respecting the calling thread's timeout.
      static void parkNanos​(Object oBlocker, long cNanos)
      Invoke LockSupport.parkNanos() while still respecting the calling thread's timeout.
      static int select​(Selector selector)
      Wait on the Selector while still respecting the calling thread's timeout.
      static int select​(Selector selector, long cMillis)
      Wait on the Selector while still respecting the calling thread's timeout.
      static void sleep​(long cMillis)
      Invoke Thread.sleep() while still respecting the calling thread's timeout.
      static void sleep​(long cMillis, int cNanos)
      Invoke Thread.sleep() while still respecting the calling thread's timeout.
      static boolean tryLock​(Lock lock, long time, TimeUnit unit)
      Attempt to acquire a lock while still respecting the calling thread's timeout.
      static void wait​(Object oMonitor)
      Wait on the the specified monitor while still respecting the calling thread's timeout.
      static void wait​(Object oMonitor, long cMillis)
      Wait on the specified monitor while still respecting the calling thread's timeout.
      static void wait​(Object oMonitor, long cMillis, int cNanos)
      Wait on the specified monitor while still respecting the calling thread's timeout.
    • Constructor Detail

      • Blocking

        public Blocking()
    • Method Detail

      • interrupted

        public static boolean interrupted()
        Return true if the thread is interrupted or timed out. Note as with Thread.interrupted this will clear the interrupted flag if it is set, it will not however clear the timeout.
        Returns:
        true if the thread is interrupted or timed out
      • wait

        public static void wait​(Object oMonitor)
                         throws InterruptedException
        Wait on the the specified monitor while still respecting the calling thread's timeout.
        Parameters:
        oMonitor - the monitor to wait on
        Throws:
        InterruptedException - if the thread is interrupted
      • wait

        public static void wait​(Object oMonitor,
                                long cMillis)
                         throws InterruptedException
        Wait on the specified monitor while still respecting the calling thread's timeout.
        Parameters:
        oMonitor - the monitor to wait on
        cMillis - the maximum number of milliseconds to wait
        Throws:
        InterruptedException - if the thread is interrupted
      • wait

        public static void wait​(Object oMonitor,
                                long cMillis,
                                int cNanos)
                         throws InterruptedException
        Wait on the specified monitor while still respecting the calling thread's timeout.
        Parameters:
        oMonitor - the monitor to wait on
        cMillis - the maximum number of milliseconds to wait
        cNanos - the additional number of nanoseconds to wait
        Throws:
        InterruptedException - if the thread is interrupted
      • sleep

        public static void sleep​(long cMillis)
                          throws InterruptedException
        Invoke Thread.sleep() while still respecting the calling thread's timeout.
        Parameters:
        cMillis - the maximum number of milliseconds to sleep
        Throws:
        InterruptedException - if the thread is interrupted
      • sleep

        public static void sleep​(long cMillis,
                                 int cNanos)
                          throws InterruptedException
        Invoke Thread.sleep() while still respecting the calling thread's timeout.
        Parameters:
        cMillis - the maximum number of milliseconds to sleep
        cNanos - the additional number of nanoseconds to sleep
        Throws:
        InterruptedException - if the thread is interrupted
      • park

        public static void park​(Object oBlocker)
        Invoke LockSupport.park() while still respecting the calling thread's timeout.
        Parameters:
        oBlocker - the blocker
      • parkNanos

        public static void parkNanos​(Object oBlocker,
                                     long cNanos)
        Invoke LockSupport.parkNanos() while still respecting the calling thread's timeout.
        Parameters:
        oBlocker - the blocker
        cNanos - the maximum number of nanoseconds to park for
      • lockInterruptibly

        public static void lockInterruptibly​(Lock lock)
                                      throws InterruptedException
        Acquire a lock while still respecting the calling thread's timeout.
        Parameters:
        lock - the lock to acquire
        Throws:
        InterruptedException - if the thread is interrupted
      • tryLock

        public static boolean tryLock​(Lock lock,
                                      long time,
                                      TimeUnit unit)
                               throws InterruptedException
        Attempt to acquire a lock while still respecting the calling thread's timeout.
        Parameters:
        lock - the lock to acquire
        time - the maximum amount of time to try for
        unit - the unit which time represents
        Returns:
        true iff the lock was acquired
        Throws:
        InterruptedException - if the thread is interrupted
      • await

        public static void await​(Condition cond,
                                 long cNanos)
                          throws InterruptedException
        Await for the Condition to be signaled while still respecting the calling thread's timeout.
        Parameters:
        cond - the condition to wait on
        cNanos - the maximum amount of time to wait
        Throws:
        InterruptedException - if the thread is interrupted
      • await

        public static void await​(Condition cond,
                                 long time,
                                 TimeUnit unit)
                          throws InterruptedException
        Await for the Condition to be signaled while still respecting the calling thread's timeout.
        Parameters:
        cond - the condition to wait on
        time - the maximum amount of time to wait
        unit - the unit which time represents
        Throws:
        InterruptedException - if the thread is interrupted
      • select

        public static int select​(Selector selector)
                          throws IOException
        Wait on the Selector while still respecting the calling thread's timeout. If the thread performing the select is interrupted, this method will return immediately and that thread's interrupted status will be set.
        Parameters:
        selector - the selector to wait on
        Returns:
        the number of keys, possibly zero, whose ready-operation sets were updated
        Throws:
        IOException - if an I/O error occurs
      • select

        public static int select​(Selector selector,
                                 long cMillis)
                          throws IOException
        Wait on the Selector while still respecting the calling thread's timeout. If the thread performing the select is interrupted, this method will return immediately and that thread's interrupted status will be set.
        Parameters:
        selector - the selector to wait on
        cMillis - the maximum amount of time to wait
        Returns:
        the number of keys, possibly zero, whose ready-operation sets were updated
        Throws:
        IOException - if an I/O error occurs
      • connect

        public static void connect​(Socket socket,
                                   SocketAddress addr)
                            throws IOException
        Connect a socket while still respecting the calling thread's timeout.
        Parameters:
        socket - the socket to connect
        addr - the address to connect to
        Throws:
        IOException - in an IO error occurs
      • connect

        public static void connect​(Socket socket,
                                   SocketAddress addr,
                                   int cMillis)
                            throws IOException
        Connect a socket within a given timeout while still respecting the calling thread's timeout.
        Parameters:
        socket - the socket to connect
        addr - the address to connect to
        cMillis - the caller specified connect timeout
        Throws:
        IOException - in an IO error occurs