Class ProducerConsumerLock

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class ProducerConsumerLock
    extends java.lang.Object
    implements java.lang.AutoCloseable
    Producer-Consumer lock implementation : (https://en.wikipedia.org/wiki/Producer%E2%80%93consumer_problem) Inspired from https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Condition.html implementation Implements AutoCloseable - When closed, all locks are unlocked.
    • Constructor Summary

      Constructors 
      Constructor Description
      ProducerConsumerLock​(int bufferCapacity)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes locks and notifies reader & producer
      void endConsume​(int units)
      Notifies writer of available units to write.
      void endProduce​(int units)
      Notifies consumer of available units to consume.
      int tryBeginConsume​(int units)
      Waits until 1..units are available for read, or lock closed.
      boolean tryBeginProduce​(int units)
      Waits until enough units are available for write, or lock closed.
      • Methods inherited from class java.lang.Object

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

      • ProducerConsumerLock

        public ProducerConsumerLock​(int bufferCapacity)
    • Method Detail

      • tryBeginProduce

        public boolean tryBeginProduce​(int units)
                                throws java.lang.InterruptedException
        Waits until enough units are available for write, or lock closed.
        Returns:
        true if enough write units reserved. false if closed.
        Throws:
        java.lang.InterruptedException
      • endProduce

        public void endProduce​(int units)
        Notifies consumer of available units to consume.
      • tryBeginConsume

        public int tryBeginConsume​(int units)
                            throws java.lang.InterruptedException
        Waits until 1..units are available for read, or lock closed.
        Returns:
        The number of available units to read (1 .. units). 0 if closed.
        Throws:
        java.lang.InterruptedException
      • endConsume

        public void endConsume​(int units)
        Notifies writer of available units to write.
      • close

        public void close()
        Closes locks and notifies reader & producer
        Specified by:
        close in interface java.lang.AutoCloseable