Producer Consumer Thread Program Java
Producer Consumer problem is one of the classic multi-threading problems in computer science and the multi-threading world. It's tricky because it involves, but it's important because most of the multi-threading problems fits into this category. There are many ways to solve producer consumer problem in Java e.g.
Hi Friends, Please help me with the Producer Consumer Problem using Thread in Java. I have written the program for Producer Consumer Problem using Thr. Nov 13, 2014 Java Producer Consumer Thread. Java Inter thread communication - Producer Consumer Problem. Programming Interview: Producer Consumer Problem in. // Java program to implement solution of producer // consumer problem. We have a synchronized block so that only a producer or a consumer thread runs at a time.
You can solve this by using wait() and notify() method, as discussed, or you can use the to solve this problem. In this article, you will learn a third way to solve the producer-consumer problem by using the BlockingQueue in Java. It is arguably the simplest way to solve this problem in any programming language because blocking queue data structure not only provides storage but also provides flow control and thread-safety, which makes the code really simple. Brian Goetz has also explained this key class and pattern in his classic book, a must read for serious Java developers. Producer Consumer Pattern using BlockingQueue Java provides a built-in blocking queue data structure in java.util.concurrent package.
It was added on JDK with multiple concurrent utilities e.g.,, and classes. The java.util.concurrent.BlockingQueue is an interface and comes with two ready-made implementations then and. As the name suggests, one is backed by an array while other is backed by linked list. In order to solve the producer-consumer problem, we will create two threads which will simulate producer and consumer and instead of shared object we will use the shared BlockingQueue.
Our code will be simple, the producer will add an element into queue and consumer will remove the element. BlockingQueue provides a put() method to store the element and take() method to retrieve the element. Both are blocking method, which means put() will block if the queue has reached its capacity and there is no place to add a new element.
Similarly, take() method will block if blocking queue is empty. So, you can see that critical requirement of the producer-consumer pattern is met right there, you don't need to put any thread synchronization code. Explanation of code If you look at above code example, you will find that we have created and started two threads and named them Producer and Consumer. The Producer thread executes the code inside, which adds 10 Integer object starting from 0. After adding each element, the Producer thread is sleeping for 200 milliseconds by calling method. This gives time to the Consumer thread to consume elements from Queue, that's why our code never blocks. Open Pit Mine Planning And Design.
You can see that our Producer and Consumer threads are working in sync because of Thread.sleep() we have introduced after put() call. You can further experiment with the code by removing the code to or inserting pause on Consumer thread to create scenarios where Queue is full or empty. Benefits of using BlockingQueue to solve Producer Consumer • Simple code, much more readable • less error prone as you don't have to deal with any external synchronization. That's all about how to solve producer consumer problem using BlockingQueue in Java. In production code, you should always use BlockingQueue, using wait() and notify() is neither easy not desirable given you have better tools available.
Even Joshua Bloch's has advised in to prefer higher concurrency utilities and libraries instead of writing your own code. Remember, the code is only written once but read numerous time for maintenance, troubleshooting and support purpose. Further Learning If you like this tutorial and hungry to learn more about thread, synchronization, and multi-threading then check out following articles as well: • Difference between notify() and notifyAll() in Java? () • The difference between synchronized block and methods in Java? () • The difference between Callable and Runnable in Java? () • Difference between extends Thread vs implements Runnable in Java?