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

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


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

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

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

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

Syntax

E pollFirst(long timeout, TimeUnit unit)

This method retrieves and removes the head element from the deque, to retrieve as well as remove an element this method waits (if necessary) until the defined waiting time to become an element available. It returns the head element, or 'null' if the defined 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 pollFirst(timeout, unit) method 

In this example we will show you how does pollFirst(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 first 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 PollFirstTimeUnit implements Runnable {
  String name;
  BlockingDeque<Integer> dq;
  public PollFirstTimeUnit(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 = 0; i < 5; i++) {
      dq.offer(i);
      try {
        System.out.println(i);
        Thread.sleep(500);
      catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }
}
class PollFirstTime implements Runnable {
  String name;
  BlockingDeque<Integer> dq;
 public PollFirstTime(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.pollFirst(1, TimeUnit.NANOSECONDS);
      System.out.println(name + " removes the element " + j);
      System.out.println("And the remaining size of deque after removing\n the first element ("+ j + ") = " + dq.size());
    catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}
public class BdqPollFirstTimeUnit {
  public static void main(String[] args) {
    BlockingDeque<Integer> dq = new LinkedBlockingDeque<Integer>(5);
    Runnable a = new PollFirstTimeUnit("A", dq);
    Runnable b = new PollFirstTime("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

0

1

2

3

4

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

Size of deque = 5

B removes the element 0

And the remaining size of deque after removing

the first element (0) = 4

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics