Java - Array Blocking Queue

Queue is used in places where one or more threads pass information or data to other threads. Basically they are used to transfer data between threads.

Queue typically orders elements in First in First out (FIFO) order. In FIFO, all new elements are added at the tail or bottom of the Queue. While retrieving data from the Queue, the element which is inserted first will be retrieved first.

Queue offers functionalities like data insertion and data retrieval.

In this article, let us see a simple example of using Array Blocking Queue.

Example :
This example has three components,
1. Producer Thread – This thread starts adding the data in to the Queue
2. Consumer Thread – This thread gets the data from the Queue whenever any data is added by the Producer.
3. Blocking Queue – This acts as an intermediate between the Producer and the Consumer thread. It gets the data or object from the producer thread and hands over to the consumer thread.

Array Blocking Queue :
Let’s create an ExecutorQueue class which has the ArrayBlockingQueue object. Any data or object can be added and retrieved from the Array blocking Queue instance.

package in.techdive.queue.demo;

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

public class ExecutorQueue
{
        public static BlockingQueue     queue   = new ArrayBlockingQueue(100);

        /**
         * Method to add Data in to the Queue
         *
         * @param Add data in Queue
         */

        public static void addDataInQueue(Object obj)
        {
                queue.add(obj);
        }

        /**
         * Get the Data from the Queue
         *
         * @return fetch Data from Queue
         * @throws InterruptedException
         */

        public static Object getDataFromQueue() throws InterruptedException
        {
                return queue.take();
        }
}

Consumer :
The below consumer Thread will wait in the Array blocking queue and retrieves the data as and when any object is added in the queue.

Note: In the Data Processing section, you can write your custom processing logic as per your requirements

package in.techdive.queue.demo;

public class ConsumerThread extends Thread
{
        public void run()
        {
                System.out.println("\nConsumerThread started...");
                boolean loop = true;
                while (loop)
                {
                        try
                        {
                                System.out.println("\nConsumerThread: Waiting to fetch data from Queue...");
                                String data = (String) ExecutorQueue.getDataFromQueue();
                                System.out.println("ConsumerThread: Got the data from Queue; Object = " + data);
                                System.out.println("ConsumerThread: Processing the data (" + data +")");
                                                               
                                /*
                                 * Data Processing section:
                                 *
                                 * Note: Write your processing logic here based on the data retrieved
                                 */

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

Producer :
The Producer is the triggering point to test Array blocking queue. It does the following operations,
1. It takes care of starting the Consumer Thread so that it will wait for any data in the Queue.
2. Soon after starting the Consumer thread, it will start adding data in to the blocking queue. Once the data is added, the Consumer thread will start retrieving the data using the queue.take() method.

package in.techdive.queue.demo;

public class ProducerApplication
{
        public static void main(String[] args)
        {

                // Start the Processor thread so that it will wait for an Object in the Blocking Queue. As soon as an Object is
                // added in the Queue, it will take and process the data
                System.out.println("ProducerApplication: Starting the Processor Thread...\n");
                ConsumerThread thread = new ConsumerThread();
                thread.start();

                ExecutorQueue.addDataInQueue("Object-1");
                ExecutorQueue.addDataInQueue("Object-2");
        }
}

Output :

ProducerApplication: Starting the Processor Thread...

ConsumerThread started...

ConsumerThread: Waiting to fetch data from Queue...
ConsumerThread: Got the data from Queue; Object = Object-1
ConsumerThread: Processing the data (Object-1)

ConsumerThread: Waiting to fetch data from Queue...
ConsumerThread: Got the data from Queue; Object = Object-2
ConsumerThread: Processing the data (Object-2)

ConsumerThread: Waiting to fetch data from Queue...

Technology: 

Search