You can also solve the producer-consumer problem by using a new Lock interface and condition variable instead of using the Synchronized Keyword and wait and notify methods. The lock provides an alternate way to achieve mutual exclusion and synchronization in Java. The advantage of a Lock over a synchronized keyword is well known, explicit locking is much more granular and powerful than a synchronized keyword, for example, the scope of a lock can range from one method to another but the scope of a synchronized keyword cannot go beyond one method. Condition variables are instance of java.util.concurrent.locks.Condition class, which provides inter-thread communication methods similar to wait, notify and notifyAll e.g. await(), signal(), and signalAll().
This post first appeared on Javarevisited: Blog About Java Programmin, please read the originial post: here