Class ProducerConsumerLock

java.lang.Object
fr.gouv.vitam.common.stream.ProducerConsumerLock
All Implemented Interfaces:
AutoCloseable

public class ProducerConsumerLock extends Object implements 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 Details

    • ProducerConsumerLock

      public ProducerConsumerLock(int bufferCapacity)
  • Method Details

    • tryBeginProduce

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

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

      public int tryBeginConsume(int units) throws 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:
      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 AutoCloseable