Package fr.gouv.vitam.common.stream

# 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 Summary

  Constructors
  Constructor
  Description
  `ProducerConsumerLock(int bufferCapacity)`

- ## Method Summary

  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 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`