Class BlockingCell<T>

  • Direct Known Subclasses:
    BlockingValueOrException

    public class BlockingCell<T>
    extends java.lang.Object
    Simple one-shot IPC mechanism. Essentially a one-place buffer that cannot be emptied once filled.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean _filled
      Indicator of not-yet-filledness
      private T _value
      Will be null until a value is supplied, and possibly still then.
      private static long INFINITY  
      private static long NANOS_IN_MILLI  
    • Constructor Summary

      Constructors 
      Constructor Description
      BlockingCell()
      Instantiate a new BlockingCell waiting for a value of the specified type.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      T get()
      Wait for a value, and when one arrives, return it (without clearing it).
      T get​(long timeout)
      Wait for a value, and when one arrives, return it (without clearing it).
      void set​(T newValue)
      Store a value in this BlockingCell, throwing IllegalStateException if the cell already has a value.
      boolean setIfUnset​(T newValue)
      Store a value in this BlockingCell if it doesn't already have a value.
      T uninterruptibleGet()
      As get(), but catches and ignores InterruptedException, retrying until a value appears.
      T uninterruptibleGet​(int timeout)
      As get(long timeout), but catches and ignores InterruptedException, retrying until a value appears or until specified timeout is reached.
      • Methods inherited from class java.lang.Object

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

      • _filled

        private boolean _filled
        Indicator of not-yet-filledness
      • _value

        private T _value
        Will be null until a value is supplied, and possibly still then.
    • Constructor Detail

      • BlockingCell

        public BlockingCell()
        Instantiate a new BlockingCell waiting for a value of the specified type.
    • Method Detail

      • get

        public T get()
              throws java.lang.InterruptedException
        Wait for a value, and when one arrives, return it (without clearing it). If there's already a value present, there's no need to wait - the existing value is returned.
        Returns:
        the waited-for value
        Throws:
        java.lang.InterruptedException - if this thread is interrupted
      • get

        public T get​(long timeout)
              throws java.lang.InterruptedException,
                     java.util.concurrent.TimeoutException
        Wait for a value, and when one arrives, return it (without clearing it). If there's already a value present, there's no need to wait - the existing value is returned. If timeout is reached and value hasn't arrived, TimeoutException is thrown.
        Parameters:
        timeout - timeout in milliseconds. -1 effectively means infinity
        Returns:
        the waited-for value
        Throws:
        java.lang.InterruptedException - if this thread is interrupted
        java.util.concurrent.TimeoutException
      • uninterruptibleGet

        public T uninterruptibleGet()
        As get(), but catches and ignores InterruptedException, retrying until a value appears.
        Returns:
        the waited-for value
      • uninterruptibleGet

        public T uninterruptibleGet​(int timeout)
                             throws java.util.concurrent.TimeoutException
        As get(long timeout), but catches and ignores InterruptedException, retrying until a value appears or until specified timeout is reached. If timeout is reached, TimeoutException is thrown. We also use System.nanoTime() to behave correctly when system clock jumps around.
        Parameters:
        timeout - timeout in milliseconds. -1 means 'infinity': never time out
        Returns:
        the waited-for value
        Throws:
        java.util.concurrent.TimeoutException
      • set

        public void set​(T newValue)
        Store a value in this BlockingCell, throwing IllegalStateException if the cell already has a value.
        Parameters:
        newValue - the new value to store
      • setIfUnset

        public boolean setIfUnset​(T newValue)
        Store a value in this BlockingCell if it doesn't already have a value.
        Parameters:
        newValue - the new value to store
        Returns:
        true if this call to setIfUnset actually updated the BlockingCell; false if the cell already had a value.