pollLast(timeout, unit) method of BlockingDeque Interface in Java.

pollLast(timeout, unit) method of BlockingDeque Interface in Java.


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

This method finds and deletes the last position element from the deque, to find as well as delete an element this method waits (if necessary) until the amount of time that defined for waiting to become the space available.

pollLast(timeout, unit) method of BlockingDeque Interface in Java.

In this section we will discuss how can pollLast(timeout, unit) method be implemented in BlockingDeque interface in java.

Syntax

E pollLast(long timeout, TimeUnit unit)

This method finds and deletes the last position element from the deque, to find as well as delete an element this method waits (if necessary) until the amount of time that defined for waiting to become the space available. It returns the last element, or 'null' if the specified waiting time lapses before an element is available.

Parameter description

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

timeout : It is the waiting time that how long to wait before giving up, in units of unit.

unit : It is the unit of time which determines how to interpret the timeout parameter. Written as 'TimeUnit.Seconds' (or, microsecond, milliseconds, nanosecond, etc).

Example of pollLast(timeout, unit) method 

In this example we will show you how does pollLast(timeout, unit) method work in BlockingDeque interface. This example will help you to understand how can you retrieve and remove the element with specified waiting time from the last position of the underlying deque.

Example :

package devmanuals.com;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
class PollLastTimeUnit implements Runnable {
  String name;
  BlockingDeque<Integer> dq;
  public PollLastTimeUnit(String name, BlockingDeque<Integer> dq) {
    this.name = name;
    this.dq = dq;
  }
  public void run() {
   System.out.println(name + " inserted the element into the deque");
    for (int i = 1; i < 6; i++) {
      dq.offer(i);
      try {
        System.out.println(i);
        Thread.sleep(500);
      catch (InterruptedException e) {
        e.printStackTrace();
     }
    }
  }
}
class PollLastTime implements Runnable {
  String name;
  BlockingDeque<Integer> dq;
  public PollLastTime(String name, BlockingDeque<Integer> dq) {
    this.name = name;
    this.dq = dq;
  }
  public void run() {
    try {
      Thread.sleep(1500);
    catch (InterruptedException e) {
      e.printStackTrace();
    }
    try {
      System.out.println("Therefore deque = " + dq);
      System.out.println("Size of deque = " + dq.size());
     int j = dq.pollLast(1, TimeUnit.NANOSECONDS);
      System.out.println(name + " removes the element " + j);
     System.out.println("And the remaining size of deque after removing \n the last element ("+ j + ") = " + dq.size());
    catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}
public class BdqPollLastTimeUnit {
  public static void main(String[] args) {
    BlockingDeque<Integer> dq = new LinkedBlockingDeque<Integer>(5);
    Runnable a = new PollLastTimeUnit("A", dq);
    Runnable b = new PollLastTime("B", dq);
    new Thread(a).start();
    try {
      Thread.sleep(1000);
    catch (InterruptedException e) {
      e.printStackTrace();
    }
    new Thread(b).start();
  }
}

Output :

A inserted the element into the deque

1

2

3

4

5

Therefore deque = [1, 2, 3, 4, 5]

Size of deque = 5

B removes the element 5

And the remaining size of deque after removing

the last element (5) = 4

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics