takeLast() method of BlockingDeque Interface in Java.

takeLast() method of BlockingDeque Interface in Java.


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

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

takeLast() method of BlockingDeque Interface in Java.

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

Syntax

E takeLast()

This method finds and removes the last position element from the underlying deque, if required this method waits for removing and deleting the elements until the elements does not become available.

Parameter Description

This method has no parameter.

Example of takeLast() method

In this example we will show you how does takeLast() method work in BlockingDeque interface. This example will help you to understand how can you find the last element of the deque.

Example :

package devmanuals.com;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
class TakeLast implements Runnable{
  BlockingDeque bdq;
   public TakeLast(BlockingDeque bdq){
    this.bdq=bdq;
  }
  public void run(){
    int i;
    System.out.println("Element of deque is inserted");
    for(i=0; i<5;i++){
      bdq.add(i);
    try{
      Thread.sleep(1000);
      System.out.println(i);
    }
    catch(InterruptedException e){
      e.printStackTrace();
    }
    }
    System.out.println("Elements of deque = "+bdq);
    System.out.println("Size of deque : "+bdq.size());
    try{ 
    int j =bdq.takeLast();
     System.out.println("The deleted element = "+j);
     System.out.println("Remaining elements of deque = "+bdq);
     System.out.println("Size of remaining deque : "+bdq.size());
  }catch(InterruptedException e){
    e.printStackTrace();
  }
  }
}
public class BdqTakeLast {
  public static void main(String args[]) {
    BlockingDeque bdq = new LinkedBlockingDeque(5);
    Runnable a = new TakeLast(bdq);
    new Thread(a).start();
  }
}

Output :

Element of deque is inserted

0

1

2

3

4

Elements of deque = [0, 1, 2, 3, 4]

Size of deque : 5

The deleted element = 4

Remaining elements of deque = [0, 1, 2, 3]

Size of remaining deque : 4

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics