Producer Consumer Using Blocking Queue:: Defining The Problem

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Producer consumer using Blocking queue :

Defining the Problem :

The Problem :
If it is asked to solve this pattern without using Blocking Queue there are 2 options

1. Locks and conditions


2. Wait and Notify

1. Locks and conditions


2. Wait and Notify
3. import java.util.LinkedList;
4. import java.util.Queue;
5. import java.util.Random;
6.
7. /**
8. * Simple Java program to demonstrate How to use wait, notify and notifyAll()
9. * method in Java by solving producer consumer problem.
10. *
11. * @author Javin Paul
12. */
13. public class ProducerConsumerInJava {
14.
15. public static void main(String args[]) {
16. System.out.println("How to use wait and notify method in Java");
17. System.out.println("Solving Producer Consumper Problem");
18.
19. Queue<Integer> buffer = new LinkedList<>();
20. int maxSize = 10;
21.
22. Thread producer = new Producer(buffer, maxSize, "PRODUCER");
23. Thread consumer = new Consumer(buffer, maxSize, "CONSUMER");
24.
25. producer.start();
26. consumer.start();
27.
28.
29. }
30.
31. }
32.
33. /**
34. * Producer Thread will keep producing values for Consumer
35. * to consumer. It will use wait() method when Queue is full
36. * and use notify() method to send notification to Consumer
37. * Thread.
38. *
39. * @author Taufik
40. *
41. */
42. class Producer extends Thread {
43. private Queue<Integer> queue;
44. private int maxSize;
45.
46. public Producer(Queue<Integer> queue, int maxSize, String name){
47. super(name);
48. this.queue = queue;
49. this.maxSize = maxSize;
50. }
51.
52. @Override
53. public void run() {
54. while (true) {
55. synchronized (queue) {
56. while (queue.size() == maxSize) {
57. try {
58. System.out .println("Queue is full, "
59. + "Producer thread waiting for "
60. + "consumer to take something from queue");
61. queue.wait();
62. } catch (Exception ex) {
63. ex.printStackTrace();
64. }
65. }
66.
67. Random random = new Random();
68. int i = random.nextInt();
69. System.out.println("Producing value : " + i);
70. queue.add(i);
71. queue.notifyAll();
72. }
73.
74. }
75. }
76. }
77.
78. /**
79. * Consumer Thread will consumer values form shared queue.
80. * It will also use wait() method to wait if queue is
81. * empty. It will also use notify method to send
82. * notification to producer thread after consuming values
83. * from queue.
84. *
85. * @author Taufik
86. *
87. */
88. class Consumer extends Thread {
89. private Queue<Integer> queue;
90. private int maxSize;
91.
92. public Consumer(Queue<Integer> queue, int maxSize, String name){
93. super(name);
94. this.queue = queue;
95. this.maxSize = maxSize;
96. }
97.
98. @Override
99. public void run() {
100. while (true) {
101. synchronized (queue) {
102. while (queue.isEmpty()) {
103. System.out.println("Queue is empty,"
104. + "Consumer thread is waiting"
105. + " for producer thread to put something in queue");
106. try {
107. queue.wait();
108. } catch (Exception ex) {
109. ex.printStackTrace();
110. }
111.
112. }
113. System.out.println("Consuming value : " + queue.remove());
114. queue.notifyAll();
115. }
116.
117. }
118. }
119. }

You might also like