Home  >  Core Java

ArrayBlockingQueue Java Example

By Arvind Rai, March 06, 2013
ArrayBlockingQueue implements BlockingQueue. ArrayBlockingQueue is bounded queue i.e. size is fixed by given number. It is backed by array. That is why it is called ArrayBlockingQueue. It acts as queue that's FIFO (first in - first out). Blocking means when the element is fetched from head and there is no element in queue, then it will wait till the element will present. We declare it as
private static BlockingQueue<String> bq = new ArrayBlockingQueue<String>(no_of_element);
We pass the number of element for the queue size. Some important ArrayBlockingQueue methods are given below

add: adds the element and does not wait for free space if space not available. It returns true or IllegalStateException.
put: adds the element in queue and waits for free space if not available.
take: takes the element from head and remove from queue.
peek: gets the element from head but not removed an returns null if not available
element: gets the element from head but not removed an throw exception if not available

Complete Example

Find the complete example. This is the producer - consumer example. put and take method will be in synch to put and get the value from the queue.
ArrayBlockingQueueTest.java
package com.concretepage;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class ArrayBlockingQueueTest {
    private static BlockingQueue<String> bq = new ArrayBlockingQueue<String>(1);
    class PutThread implements Runnable {
        @Override
        public void run() {
            try {
                int i = 0;
                while (true) {
                    bq.put("A" + i);
                    System.out.println("Producer has put: A" + i);
                    i++;
                    Thread.sleep(1000);
                }
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }
    }
    class TakeThread implements Runnable {
        @Override
        public void run() {
            try {
                while (true) {
                    final String data = bq.take();
                    System.out.println("Consumer has consumed.." + data);
                }
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(final String... args) {
        final Thread pt = new Thread(new ArrayBlockingQueueTest().new PutThread());
        pt.start();

        final Thread tt = new Thread(new ArrayBlockingQueueTest().new TakeThread());
        tt.start();
    }
} 
Output
Producer has put: A0
Consumer has consumed..A0
Producer has put: A1
Consumer has consumed..A1
Producer has put: A2
Consumer has consumed..A2
Producer has put: A3
Consumer has consumed..A3
Producer has put: A4
Consumer has consumed..A4
--------------------------
-------------------------
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us