poll() method of BlockingDeque Interface in Java.

poll() method of BlockingDeque Interface in Java.


Posted in : Core Java Posted on : March 4, 2011 at 4:46 PM Comments : [ 0 ]

This method is different from the remove() method only as the remove() method throws 'NoSuchElementException' if the underlying deque is empty whereas the poll() method returns 'null'.� 

poll() method of BlockingDeque Interface in Java.

In this section we will discuss how can poll( ) method be implemented in BlockingDeque interface in java.

Syntax

E poll()

This method retrieves as well as removes the head element from the underlying deque or, returns 'null' if deque is empty.

This method is different from the remove() method only as the remove() method throws 'NoSuchElementException' if the underlying deque is empty whereas the poll() method returns 'null'.

Parameter description 

This method has no argument.

E : It is the type of parameter in which the element has to be returned.

Example of poll() method

In this example we will show you how does poll() method work in BlockingDeque interface. This example will help you to understand how can you retrieve a head element and removed that from the underlying deque. Further we will examine that is the currently removed element is still exist into the deque or, not ?

Example :

package devmanuals.com;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
class Poll implements Runnable {
  BlockingDeque<Integer> bdq;
  public Poll(BlockingDeque<Integer> bdq) {
    this.bdq = bdq;
  }
  public void run() {
    System.out.println("Elements of deque are : ");
    int i;
    boolean bol = false;
    for (i = 0; i < 5; i++) {
        bdq.add(i);
      }
      System.out.println(bdq);
      System.out.println("Size of deque = "+ bdq.size());
    try {
      Thread.sleep(500);
      System.out.println("The first element of deque is = ");
      Thread.sleep(1000);
      Integer obj = bdq.poll();
      System.out.println(obj);
      bol = bdq.contains(obj);
      System.out.println("The element " + obj" is still present into the deque? ");
      Thread.sleep(1000);
      System.out.println(bol);
      System.out.println("Now removing all the elements from the deque");
      bdq.clear();
      System.out.println("Therefore the deque is "+bdq+" empty");
      obj=bdq.poll();//Calling of this method will return 'null'
      System.out.println("The head element = "+obj);      
    catch (InterruptedException e) {
      e.getMessage();
    }
  }
}
public class BdqPoll {
  public static void main(String[] args) {
    BlockingDeque<Integer> bdq = new LinkedBlockingDeque<Integer>();
    Runnable a = new Poll(bdq);
    new Thread(a).start();
  }
}

Output :

Elements of deque are :

[0, 1, 2, 3, 4]

Size of deque = 5

The first element of deque is =

0

The element 0 is still present into the deque?

false

Now removing all the elements from the deque

Therefore the deque is [] empty

The head element = null

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics