element() method of BlockingDeque Interface in Java.

element() method of BlockingDeque Interface in Java.


Posted in : Core Java Posted on : February 23, 2011 at 4:09 PM Comments : [ 0 ]

In other word we can say it gives back the first element of the underlying deque. This method is little bit different from the peek() method that it displays

element() method of BlockingDeque Interface in Java.

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

Syntax

E element()

This method retrieves the head element from the underlying deque but, it can't remove that element.

In other word we can say it gives back the first element of the underlying deque. This method is little bit different from the peek() method that it displays 'NoSuchElementException' exception if deque is empty. 

Parameter description 

E : It is the type parameter in deque.

This method has no argument.

Example of element( ) method 

In this example we will show you, how does element( ) method work in BlockingDeque interface. In the following example we will show how can you retrieve the element at the head position of the underlying Deque, and count the total number of elements in deque. Further we will also show you what the exception it displays when the underlying deque will be empty. 

Example :

package devmanuals.com;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
class Element implements Runnable {
  BlockingDeque<Integer> bdq;
  public Element(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.element();
      System.out.println(obj);
      bol = bdq.contains(4);
      System.out.println("The element " + obj" is still present into the deque? ");
      Thread.sleep(1000);
      System.out.println(bol);
      bdq.clear();
      System.out.println("The first element of deque is : ");
      Integer obj1 = bdq.element();// This call of element()method will
                      // displayed the exception
      Thread.sleep(2000);
      System.out.println(obj1);
    catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}
public class BdqElement {
  public static void main(String[] args) {
    BlockingDeque<Integer> bdq = new LinkedBlockingDeque<Integer>();
    Runnable a = new Element(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?

true

The first element of deque is :

Exception in thread "Thread-0" java.util.NoSuchElementException

at java.util.concurrent.LinkedBlockingDeque.getFirst(Unknown Source)

at java.util.concurrent.LinkedBlockingDeque.element(Unknown Source)

at devmanuals.com.Element.run(BdqElement.java:35)

at java.lang.Thread.run(Unknown Source)

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics