Have a Thread wait for a Condition
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?
What’s wrong with just waiting for response (like socketInputStream.read()), until you get full response and then acting on it? That’s what worker threads are for. Why do you have extra thread for listening for responses? To your question . wait (on worker thread)/notify (observing thread) should work fine.
I can’t do just a read because I have a seperate thread managing the socket. That seperate thread is needed because the server may send back responses when I’m not waiting for one. For example, the server may send a «response» that it is shutting down. Without that listening thread my client/GUI would not be notified until it tries to interact with the server.
2 Answers 2
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. «
I was looking at the CountDownLatch before, but that seemed like it was more geared for multiple threads. For now though, I believe that will work. Thanks!
This doesn’t seem like a place you want to block at all. You are already in a worker thread so you aren’t holding any kind of state for your GUI or anything, I’d send the message and bail—when your response comes back, grab another thread out of the pool and send it on it’s merry way.
If you have data that is shared between the two, put it into its own object in a collection keyed by some common value and have the new thread pull it out when it comes in.
For readability/simplicity make the object with the data also contain the code that knows how to handle that data so that all you have to do when you get an incoming message is retrieve a key that uniquely identifies which object sent it, retrieve that object by the key and call that object’s «Data received» method passing in the information you got from the packet.
The nice thing about this is it makes it trivial for a simple listener to handle a huge variety of incoming packets by making the «Data received» method an interface that many different handlers can implement.
Interface Condition
Condition factors out the Object monitor methods ( wait , notify and notifyAll ) into distinct objects to give the effect of having multiple wait-sets per object, by combining them with the use of arbitrary Lock implementations. Where a Lock replaces the use of synchronized methods and statements, a Condition replaces the use of the Object monitor methods.
Conditions (also known as condition queues or condition variables) provide a means for one thread to suspend execution (to «wait») until notified by another thread that some state condition may now be true. Because access to this shared state information occurs in different threads, it must be protected, so a lock of some form is associated with the condition. The key property that waiting for a condition provides is that it atomically releases the associated lock and suspends the current thread, just like Object.wait .
A Condition instance is intrinsically bound to a lock. To obtain a Condition instance for a particular Lock instance use its newCondition() method.
As an example, suppose we have a bounded buffer which supports put and take methods. If a take is attempted on an empty buffer, then the thread will block until an item becomes available; if a put is attempted on a full buffer, then the thread will block until a space becomes available. We would like to keep waiting put threads and take threads in separate wait-sets so that we can use the optimization of only notifying a single thread at a time when items or spaces become available in the buffer. This can be achieved using two Condition instances.
class BoundedBuffer < final Lock lock = new ReentrantLock(); final Condition notFull = lock.newCondition(); final Condition notEmpty = lock.newCondition(); final Object[] items = new Object[100]; int putptr, takeptr, count; public void put(E x) throws InterruptedException < lock.lock(); try while (count == items.length) notFull.await(); items[putptr] = x; if (++putptr == items.length) putptr = 0; ++count; notEmpty.signal(); > finally > public E take() throws InterruptedException < lock.lock(); try while (count == 0) notEmpty.await(); E x = (E) items[takeptr]; if (++takeptr == items.length) takeptr = 0; --count; notFull.signal(); return x; > finally > >
(The ArrayBlockingQueue class provides this functionality, so there is no reason to implement this sample usage class.)
A Condition implementation can provide behavior and semantics that is different from that of the Object monitor methods, such as guaranteed ordering for notifications, or not requiring a lock to be held when performing notifications. If an implementation provides such specialized semantics then the implementation must document those semantics.
Note that Condition instances are just normal objects and can themselves be used as the target in a synchronized statement, and can have their own monitor wait and notify methods invoked. Acquiring the monitor lock of a Condition instance, or using its monitor methods, has no specified relationship with acquiring the Lock associated with that Condition or the use of its waiting and signalling methods. It is recommended that to avoid confusion you never use Condition instances in this way, except perhaps within their own implementation.
Except where noted, passing a null value for any parameter will result in a NullPointerException being thrown.
Implementation Considerations
When waiting upon a Condition , a «spurious wakeup» is permitted to occur, in general, as a concession to the underlying platform semantics. This has little practical impact on most application programs as a Condition should always be waited upon in a loop, testing the state predicate that is being waited for. An implementation is free to remove the possibility of spurious wakeups but it is recommended that applications programmers always assume that they can occur and so always wait in a loop.
The three forms of condition waiting (interruptible, non-interruptible, and timed) may differ in their ease of implementation on some platforms and in their performance characteristics. In particular, it may be difficult to provide these features and maintain specific semantics such as ordering guarantees. Further, the ability to interrupt the actual suspension of the thread may not always be feasible to implement on all platforms.
Consequently, an implementation is not required to define exactly the same guarantees or semantics for all three forms of waiting, nor is it required to support interruption of the actual suspension of the thread.
An implementation is required to clearly document the semantics and guarantees provided by each of the waiting methods, and when an implementation does support interruption of thread suspension then it must obey the interruption semantics as defined in this interface.
As interruption generally implies cancellation, and checks for interruption are often infrequent, an implementation can favor responding to an interrupt over normal method return. This is true even if it can be shown that the interrupt occurred after another action that may have unblocked the thread. An implementation should document this behavior.