Suppose I have two main running threads A and B, and one thread that is called asynchronously T. I want thread A to wait until a message aMsg is received on thread T and thread B to stop until message msgB is received on thread T. I know how to do this with 2 sempahores:
sempahoreA = new Sempahore(0);
sempahoreB = new Sempahore(0);
//in thread A
//code until where to run
semaphoreA.acquire()
//in thread B
//code until where to run
semaphoreB.acquire()
//in thread T
if (msgA.equals(msgRecevied)) {
semaphoreA.release()
}
if (msgB.equals(msgReceived)) {
semaphoreB.release()
}
The trouble is that I have multiple A,B,C,... threads and I do not want to use multiple semaphores. Is there a class somewhere in java.util.concurrent
that can replace all the semaphores with only one instance?
synchronizer = //?
//in thread A
//code until where to run
synchronizer.acquire(msgA)//only let go if msgA is received from thread calling release
//in thread B
//code until where to run
synchronizer.acquire(msgB)//only let go if msgA is received from thread calling release
//in thread T
if (msgA.equals(msgRecevied)) {
synchronizer.release(msgA)
}
if (msgB.equals(msgReceived)) {
synchronizer.release(msgB)
}//actually here you can call synchronizer.release(msgReceived)