Interview Questions

How can you solve consumer producer pattern by using BlockingQueue?

Java Interview Questions and Answers (part 4)


(Continued from previous question...)

19. How can you solve consumer producer pattern by using BlockingQueue?

Now it’s time to gear up to face question which is most probably going to be followed up by previous question i.e. after how to solve consumer producer problem using wait() and notify() method. Generally you might wonder why interviewer's are so much interested in asking about solving consumer producer problem using BlockingQueue, answer is they want to know how strong knowledge you have about java concurrent Api’s, this Api use consumer producer pattern in very optimized manner, BlockingQueue is designed is such a manner that it offer us the best performance.

BlockingQueue is a interface and we will use its implementation class LinkedBlockingQueue.

Key methods for solving consumer producer pattern are >

put(i); //used by producer to put/produce in sharedQueue.
take(); //used by consumer to take/consume from sharedQueue.

(Continued on next question...)

Other Interview Questions