0% found this document useful (0 votes)
9 views

Blocking Queue Demo

Uploaded by

muralichadive
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Blocking Queue Demo

Uploaded by

muralichadive
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

package com.engineeringdigest.

collectionframework;

import java.util.Comparator;

import java.util.concurrent.*;

class Producer implements Runnable {

private BlockingQueue<Integer> queue;

private int value = 0;

public Producer(BlockingQueue<Integer> queue) {

this.queue = queue;

@Override

public void run() {

while (true) {

try {

System.out.println("Producer produced: " + value);

queue.put(value++);

Thread.sleep(1000);

} catch (Exception e) {

Thread.currentThread().interrupt();

System.out.println("Producer interrupted");

}
}

class Consumer implements Runnable {

private BlockingQueue<Integer> queue;

public Consumer(BlockingQueue<Integer> queue) {

this.queue = queue;

@Override

public void run() {

while (true) {

try {

Integer value = queue.take();

System.out.println("Consumer consumed: " + value);

Thread.sleep(2000);

} catch (Exception e) {

Thread.currentThread().interrupt();

System.out.println("Consumer interrupted");

public class BockingQueueDemo {

public static void main(String[] args) {

BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(5);


Thread producer = new Thread(new Producer(queue));

Thread consumer = new Thread(new Consumer(queue));

producer.start();

consumer.start();

BlockingQueue<Integer> queue1 = new LinkedBlockingQueue<>();

BlockingQueue<String> queue2 = new PriorityBlockingQueue<>(11, Comparator.reverseOrder(

queue2.add("apple");

queue2.add("banana");

queue2.add("cherry");

System.out.println(queue2);

BlockingQueue<Integer> queue3 = new SynchronousQueue<>();

You might also like