Package fr.gouv.vitam.common.stream
Class ProducerConsumerLock
- java.lang.Object
-
- fr.gouv.vitam.common.stream.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 ImplementsAutoCloseable
- 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 & 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.
-
-
-
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 interfacejava.lang.AutoCloseable
-
-