Package fr.gouv.vitam.common.stream
Class ProducerConsumerLock
java.lang.Object
fr.gouv.vitam.common.stream.ProducerConsumerLock
- All Implemented Interfaces:
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
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes locks and notifies reader & producervoid
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.
-
Constructor Details
-
ProducerConsumerLock
public ProducerConsumerLock(int bufferCapacity)
-
-
Method Details
-
tryBeginProduce
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
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 interfaceAutoCloseable
-