A blocking queue is a queue that blocks the queue when you try to dequeue items from it. If the queue is empty. Or if you try to enqueue items into it when the queue is already full. A thread trying to dequeue from an empty queue is blocked until some other thread inserts an item into the queue.
so we don't need to implement wait and notify manually.
Example-1
Related Tutorial
so we don't need to implement wait and notify manually.
Example-1
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class ProducerConsumerUsingBlickingQueue {
public static void main(String args[]) {
// Creating shared object
BlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>();
// Creating Producer
Thread prodThread = new Thread(new Producer(queue));
// Consumer Thread
Thread consThread = new Thread(new Consumer(queue));
// Starting producer and Consumer thread
prodThread.start();
consThread.start();
}
}
class Producer implements Runnable {
private final BlockingQueue<Integer> queue;
public Producer(BlockingQueue<Integer> queue) {
this.queue = queue;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
System.out.println("Produced: " + i);
queue.put(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable {
private final BlockingQueue<Integer> queue;
public Consumer(BlockingQueue<Integer> queue) {
this.queue = queue;
}
@Override
public void run() {
while (true) {
try {
System.out.println("Consumed: " + queue.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Output of above program:-
Produced: 0
Produced: 1
Produced: 2
Consumed: 0
Produced: 3
Consumed: 1
Produced: 4
Consumed: 2
Consumed: 3
Produced: 5
Consumed: 4
Produced: 6
Produced: 7
Produced: 8
Consumed: 5
Produced: 9
Consumed: 6
Consumed: 7
Consumed: 8
Consumed: 9
Example-2
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class ProducerConsumerUsingBlickingQueue2 {
public static void main(String args[]) {
// Creating shared object
BlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>();
// Creating Producer
Thread prodThread = new Thread(new Producer(queue));
// Consumer Thread
Thread consThread = new Thread(new Consumer(queue));
// Starting producer and Consumer thread
prodThread.start();
consThread.start();
}
}
class Producer implements Runnable {
private final BlockingQueue<Integer> queue;
public Producer(BlockingQueue<Integer> queue) {
this.queue = queue;
}
@Override
public void run() {
int value = 0;
while (true) {
try {
System.out.println("Produced: " + value);
queue.put(value);
value++;
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable {
private final BlockingQueue<Integer> queue;
public Consumer(BlockingQueue<Integer> queue) {
this.queue = queue;
}
@Override
public void run() {
while (true) {
try {
System.out.println("Consumed: " + queue.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Output of above program:-
Produced: 0
Consumed: 0
Produced: 1
Consumed: 1
Produced: 2
Consumed: 2
Produced: 3
Consumed: 3
Produced: 4
Consumed: 4
Produced: 5
Consumed: 5
Produced: 6
Consumed: 6
Produced: 7
Consumed: 7
Produced: 8
Consumed: 8
Produced: 9
Consumed: 9
Produced: 10
Consumed: 10
Produced: 11
Consumed: 11
Produced: 12
Consumed: 12
So from the above example its clear the use of BlockingQueue in Producer Consumer without using wait and notify methods of object class.
Related Tutorial
No comments:
Post a Comment