It is said, that ReentrantReadWriteLock is intended for one writer and multiple readers.
Nevertheless, readers should wait until some data is present in the buffer.
So, what to lock?
I created concurrency objects like follows:
private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
protected final Lock readLock = rwl.readLock();
protected final Lock writeLock = rwl.writeLock();
protected final Condition hasData = writeLock.newCondition();
now in write method I do:
writeLock.lock();
// writing first portion and updating variables
hasData.signalAll();
// if required then writing second portion and updating variables
hasData.signalAll();
But how to write a reader? Should it acquire only readLock? But how it can wait for a signal then? If it aquires also a writeLock then where is the supremacy fo read/write locking?
How to ensure required variables will not change during reading if they are protected only by writeLock?
QUEUES DON'T MATCH THE TASK
This is the question about ReentrantReadWriteLock.
The ReentrantReadWriteLock is indeed a bit confusing because the readLock doesn't have a condition. You have to upgrade to a writeLock in your reader only to wait for the condition.
In the writer.
In reader you do:
For completeness, each unlock() should be in a finally block, of course.