Como esperar por dados com ReentrantReadWriteLock?

Dizem queReentrantReadWriteLock destina-se a um escritor e vários leitores.

No entanto, os leitores devem esperar até que alguns dados estejam presentes no buffer.

Então, o que bloquear?

Eu criei objetos de concorrência como segue:

private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
protected final Lock readLock = rwl.readLock();
protected final Lock writeLock = rwl.writeLock();
protected final Condition hasData = writeLock.newCondition();

agora no método write eu faço:

writeLock.lock();

// writing first portion and updating variables

hasData.signalAll();

// if required then writing second portion and updating variables

hasData.signalAll();

Mas como escrever um leitor? Deve adquirir apenasreadLock? Mas como pode esperar por um sinal então? Se ele também adquirirwriteLock então onde está a supremacia do bloqueio de leitura / gravação?

Como garantir que as variáveis ​​requeridas não serão alteradas durante a leitura se estiverem protegidas apenas porwriteLock?

FILAS NÃO CORRESPONDEM À TAREFA

Esta é a pergunta sobreReentrantReadWriteLock.

questionAnswers(3)

yourAnswerToTheQuestion