remove() method of BlockingDeque Interface in Java.

remove() method of BlockingDeque Interface in Java.


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

This method is little bit different from the poll() method as it throws 'NoSuchElementException' if an existing deque has no element whereas, poll() method returns 'null'. This method is similar to the removeFirst() method.

remove() method of BlockingDeque Interface in Java.

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

Syntax

E remove()

This method retrieves and deletes the first position element from the underlying deque.

This method is little bit different from the poll() method as it throws 'NoSuchElementException' if an existing deque has no element whereas, poll() method returns 'null'. This method is similar to the removeFirst() method.

Parameter description

This method has no argument but, it returns the front element of the deque.

Example of remove( ) method 

In this example we will show you how does remove() method work in BlockingDeque interface. Through this example we will show how can you delete the head element of the underlying deque, and count the total number of elements in deque before and after removing the elements.

Example :

package devmanuals.com;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
class Remove implements Runnable {
  BlockingDeque<Integer> bdq;
  String name;
  public Remove(String name, BlockingDeque<Integer> bdq) {
    this.bdq = bdq;
    this.name = name;
  }
  public void run() {
    int i;
    System.out.println(name + " inserted the element into the deque");
    for (i = 0; i < 5; i++) {
      bdq.add(i);
      System.out.println(i);
      try {
        Thread.sleep(400);
      catch (InterruptedException e) {
        e.printStackTrace();
     }
    }
    bdq.addFirst(3);
    System.out.println(name" adds the new element at first position then deque = "+ bdq);
    System.out.println("Size of deque = " + bdq.size());
  }
}
class Remove1 implements Runnable {
  String name;
  BlockingDeque<Integer> bdq;
  public Remove1(String name, BlockingDeque<Integer> bdq) {
    this.bdq = bdq;
    this.name = name;
    // new Thread().start();
  }
  public void run() {
    try {
      bdq.remove();
      Thread.sleep(1500);
      System.out.println(name" deleted the first element then remaining deque = "+ bdq);
      System.out.println("Size of deque = " + bdq.size());
    catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}
public class BdqRemove {
  public static void main(String args[]) {
    BlockingDeque<Integer> bdq = new LinkedBlockingDeque<Integer>(5);
    Runnable a = new Remove("A", bdq);
    new Thread(a).start();
    try {
      Thread.sleep(400);
    catch (InterruptedException e) {
      e.printStackTrace();
    }
    Runnable b = new Remove1("B", bdq);
    new Thread(b).start();
  }
}

Output :

A inserted the element into the deque

0

1

2

3

4

B deleted the first element then remaining deque = [1, 2, 3, 4]

Size of deque = 4

A adds the new element at first position then deque = [3, 1, 2, 3, 4]

Size of deque = 5

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics