Wait on multiple Conditions in Java
I want a thre开发者_如何学JAVAad to wait until any of two Conditions are signaled. Is it possible?
Suppose the thread is handling incoming packets of data, while it's possible a user interrupts by changing some states.
One Condition is signaled when a packet has come, and the other is signaled when states have been changed.
I think it's inappropriate to use one Condition for both events, because that would cause a lot of unnecessary wakeups in other threads which are monitoring the states.
You can add a third condition that is signaled by both incoming packets and user state changes? I don't think it is possible to wait on multiple conditions.
Use a semaphore, with 2 permits, have your waiting item acquire those 2, and have your events release those permits, 1 at a time until 2 are back?
精彩评论