Skip to main content

Posts

Showing posts with the label Producer consumer BlockingQueue

BlockingQueue for producer and consumer problem

How BlockingQueue fit into Solution Any effective solution of producer consumer problem has to control the invocation of produce’s put() method which generates the resource – and consumer’s take() method which consumes the resource. Once you achieve this control of blocking the methods, you have solved the problem. Java provides out of the box support to control such method invocations where one thread is creating resources and other is consuming them- through  BlockingQueue . The Java  BlockingQueue  interface in the  java.util.concurrent  package represents a queue which is thread safe to put into, and take instances from. BlockingQueue is a construct where one thread putting resources into it, and another thread taking from it. This is exactly what is needed to solve the producer consumer problem. Let’s solve the problem now !! Using BlockingQueue to solve Producer Consumer problem Producer Below code is for producer thread. class ...