Thursday 11 September 2014

ArrayBlockingQueue

It is a bounded blocking queue backed by an Array. ArrayBlockingQueue class provides below constructors to instantiate this class.

public ArrayBlockingQueue(int capacity)
Cretaes ArrayBlockingQueue with given capacity and default access policy.

public ArrayBlockingQueue(int capacity, boolean fair)
Creates an ArrayBlockingQueue with the given capacity and the specified access policy. if 'fair' set to true then queue accesses for threads blocked on insertion or removal, are processed in FIFO order; if false the access order is unspecified.

public ArrayBlockingQueue(int capacity, boolean fair, Collection<? extends E> c)
Creates an ArrayBlockingQueue with the given capacity, the specified access policy and initially containing the elements of the given collection, added in traversal order of the collection's iterator.

Example
import java.util.concurrent.ArrayBlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Producer implements Runnable{
    ArrayBlockingQueue<Integer> queue;
    
    Producer(ArrayBlockingQueue q){
        queue = q;
    }
    
    @Override
    public void run(){
        int i=0;
        
        while(true){
            System.out.println(Thread.currentThread().getName() +":" + i);
            try {
                queue.put(i);
            } catch (InterruptedException ex) {
                Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
            }
            i++;
        }
    }
}

import java.util.concurrent.ArrayBlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Consumer implements Runnable{
    ArrayBlockingQueue queue;
    
    Consumer(ArrayBlockingQueue q){
        queue = q;
    }
    
    public void run(){
        while(true){
            System.out.print(Thread.currentThread().getName() +":");
            try {
                System.out.println(queue.take());
            } catch (InterruptedException ex) {
                Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

import java.util.concurrent.ArrayBlockingQueue;

public class ArrayBlockingQEx {
    public static void main(String args[]){
        ArrayBlockingQueue q = new ArrayBlockingQueue(3, true);
        
        Producer p1 = new Producer(q);
        Producer p2 = new Producer(q);
        
        Consumer con = new Consumer(q);
        
        Thread t1 = new Thread(p1);
        t1.setName("P1");
        Thread t2 = new Thread(p2);
        t2.setName("P2");
        
        Thread t3 = new Thread(con);
        t3.setName("Con");
        
        t1.start();
        t2.start();
        t3.start();
    }
}

Sample Output
P1:0
Con:P2:0
P1:1
0
P1:2
P2:1
P1:3
Con:0
P2:2
Con:1
Con:2
Con:P2:3
1
Con:P2:4





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment