Wednesday, 17 September 2014

BlockingDeque

Deque stands for Double Ended Queue, where you can perform insertions, deletions from both the ends of the queue. A BlockingDeque is a Deque that additionally supports blocking operations that wait for the deque to become non-empty when retrieving an element, and wait for space to become available in the deque when storing an element.

BlockingDeque provides 4 set of methods to insert, remove and examine the element in Queue. Each set of methods behave differently, if they are unable to perform the operation at given point of time like throwing exception, waiting infinitely, waiting finitely or by returning special value.



Throws exception Special value Blocks Times out
Insert addFirst(e) offerFirst(e) putFirst(e) offerFirst(e, time, unit)
Remove removeFirst() pollFirst() takeFirst() pollFirst(time, unit)
Examine getFirst() peekFirst() not applicable not applicable



Throws exception Special value Blocks Times out
Insert addLast(e) offerLast(e) putLast(e) offerLast(e, time, unit)
Remove removeLast() pollLast() takeLast() pollLast(time, unit)
Examine getLast() peekLast() not applicable not applicable

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

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

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

public class Consumer implements Runnable{
    BlockingDeque<Integer> queue;
    
    Consumer(BlockingDeque q){
        queue = q;
    }
    
    @Override
    public void run(){
        while(true){
            try {
                Thread.sleep(1000);
                System.out.println("Consumer : " + queue.take());
            } catch (InterruptedException ex) {
                Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
            }
     
        }
    }
}

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

public class BlockingDequeEx {
    public static void main(String args[]){
        BlockingDeque<Integer> q;
        
        q = new LinkedBlockingDeque<> (5);
        
        Producer p1 = new Producer(q);
        
        Consumer con = new Consumer(q);
        
        Thread t1 = new Thread(p1);        
        Thread t2 = new Thread(con);
        
        t1.start();
        t2.start();     
    }
}

Output
Producer :1
Producer :2
Producer :3
Producer :4
Producer :5
Consumer : 1
Producer :6
Consumer : 2
Producer :7
Consumer : 3
Producer :8
Producer :9
Consumer : 4
Producer :10
Consumer : 5





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment