Java program for producer consumer problem

Java program for producer consumer problem

Producer consumer problem is an important design concept involving multi-threading and inter thread communication. The concept is that, the producer has to produce an element and notify the consumer to consume it. Before producing the next element the producer has to wait until the previously produced element is consumed by the consumer. Similarly before consuming, the consumer has to wait until an element is being produced. It has to consume the element as soon as it is available and notify the producer to produce the next element. In this post we will see Java program for producer consumer problem. This is an important Java interview question in Thread.

Producer consumer problem in Java

Producer
Consumer
ProducerConsumerTest
In this program we have used wait(), notify() methods for inter thread communication. We should be very cautious while writing multi threaded programming. If proper synchronization and inter thread communication is not implemented, the program may behave unexpectedly.

Hope you find the post useful. If you have any comments, post it in the comments section.

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.

Add Comment

Required fields are marked *. Your email address will not be published.