Provide Best Programming Tutorials

Blocking Queues

Blocking Queues

Java Collections Framework provides ArrayBlockingQueue, LinkedBlockingQueue,and PriorityBlockingQueue for supporting blocking queues.

A blocking queue causes a thread to block when you try to add an element to a full queue or to remove an element from an empty queue.

The BlockingQueue interface extends java.util.Queue and provides the synchronized put and take methods for adding an element to the tail of the queue and for removing an element from the head of the queue.

Three concrete blocking queues—ArrayBlockingQueue , LinkedBlockingQueue ,and PriorityBlockingQueue—are provided in Java. All are in the java.util.concurrent package. ArrayBlockingQueue implements a blocking queue using an array. You have to specify a capacity or an optional fairness to construct an ArrayBlockingQueue . LinkedBlockingQueue implements a blocking queue using a linked list. You can create an unbounded or bounded LinkedBlockingQueue.PriorityBlockingQueue is a priority queue. You can create an unbounded or bounded priority queue.

Next let’s see how we can do producer/consumer before using blocking queue.

package multithreading.blockingQueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class BlockingQueueDemo {
    private static BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue(1);

    public static void main(String[] args) {

        ExecutorService executorService = Executors.newFixedThreadPool(2);
        executorService.execute(new ProducerTask());
        executorService.execute(new ConsumerTask());


    }

    public static class ProducerTask implements Runnable {

        @Override
        public void run() {
            try {
                while (true) {
                    System.out.println("before produce:" + blockingQueue);
                    blockingQueue.put(1);
                    System.out.println("after produce:" + blockingQueue);
                    Thread.sleep(500);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static class ConsumerTask implements Runnable {

        @Override
        public void run() {
            try {
                while (true) {
                    System.out.println("before consume:" + blockingQueue);
                    blockingQueue.take();
                    System.out.println("after consume:" + blockingQueue);
                    Thread.sleep(500);
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

You used locks and conditions to synchronize the Producer and Consumer threads. This program does not use locks and conditions, because synchronization is already implemented in ArrayBlockingQueue .

Questions

  1. What is a blocking queue? What blocking queues are supported in Java?

  2. What method do you use to add an element to an ArrayBlockingQueue ? What happens if the queue is full?

  3. What method do you use to retrieve an element from an ArrayBlockingQueue ?What happens if the queue is empty?

Leave a Reply

Close Menu