In a GUI, I have a few buttons. These buttons spin off worker threads that send requests over the network to a server. In a seperate thread, there is a listener that receives the responses from the server. This response is passed to the same object that the worker threads are executing methods on via the Observer/Observable interface.
What I need to do is to have the worker threads wait for a response from the server that pertains to them. Essentially a worker thread should send the command, then wait for some condition that indicates the right response was received. I can think of multiple ways to do this (sleeping, polling, wait, notify, monitors, etc), but is there a particular method that would be best in this situation?
I would recommend to use a high level “locking” mechanism from the java.util.concurrent package
Eg a CountDownLatch — “A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes. “