Abhi On Java: Java 5 Concurrency: Conditions
As mentioned in the previous post Java 5 Locks, the standard wait, notify and notifyall methods do not allow multiple wait sets per object. The Java 5 condition object by factoring out these methods into distinct objects to give the effect of having multiple wait sets per object. Conditions provide a means for one thread to suspend execution (to "wait") until notified by another thread that some state condition may now be true. 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.Skip to Sample Code
The Condition interface describes condition variables that may be associated with Locks. These are similar in usage to the implicit monitors accessed using Object.wait(), but offer extended capabilities. In particular, multiple Condition objects may be associated with a single Lock. The following example demonstrates the use of Conditions through an implementation of the Producer/Consumer problem (Bounded buffer problem). This example is similar to the one in the reader-writer locks example
Read full article from Abhi On Java: Java 5 Concurrency: Conditions
No comments:
Post a Comment