I'm using a ReentrantLock
along with a Condition
to synchronise two threads. Whenever the consumer threads performs an await()
on the condition before the producer thread performs a signal()
, things seem to work fine. However, when the producer thread performs a signal()
before the consumer thread performs an await()
, then the consumer thread just ends up waiting for ever. Is this supposed to happen or am I doing something wrong? What is the expected behaviour when a condition is signalled and there are no waiting threads?
Here's the code for the consumer:
this.lock.lock();
Log.d("websocket", "Sender acquired lock");
try
{
Log.d("websocket", "Sender waiting for message from server");
this.resultReady.await();
Log.d("websocket", "Sender waking up");
return this.result;
} catch (InterruptedException e)
{
e.printStackTrace();
}
finally {
this.lock.unlock();
}
Here's the code for the producer:
Log.d("websocket", "Receiver acquiring lock");
this.lock.lock();
Log.d("websocket", "Receiver acquired lock");
try
{
Log.d("websocket", "Receiver setting result");
result = serviceResult;
Log.d("websocket", "Receiver waking up waiting threads");
this.resultReady.signal();
} finally
{
this.lock.unlock();
}