You need to use a
wait and notify[
^] structure.
Simply put the when the sender gets to an empty queue, it goes into a wait state, and when the receiver adds something to the queue it notifies.
If all the Senders and Receivers are running within the same JVM, then you can ensure that someone gets the lock by putting all the logic in Synchronized blocks and using
notifyAll()
to wake up ALL the threads, rather than
notify()
which will wake up one and only one thread.