offerLast(e) method of BlockingDeque Interfrace in Java.

offerLast(e) method of BlockingDeque Interfrace in Java.


Posted in : Core Java Posted on : March 3, 2011 at 8:00 PM Comments : [ 0 ]

This method is preferably used when we have a capacity limitation with deque. It inserts an element to the underlying deque if it is possible to do so instantly without breaking the capacity limitation of the deque. The addLast(e) method is different from this method only by their behavior, as the addLast(e) method throws an 'IllegalStateException' if the addition operation get failed whereas, offerLast(e) method returns 'false' and returns 'true' on successful operation.

offerLast(e) method of BlockingDeque Interfrace in Java.

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

Syntax

boolean offerLast(E e)

This method inserts the specified element at the last position into the deque and returns 'true' if currently space is available otherwise, returns 'false'.

This method is preferably used when we have a capacity limitation with deque. It inserts an element to the underlying deque if it is possible to do so instantly without breaking the capacity limitation of the deque. The addLast(e) method is different from this method only by their behavior, as the addLast(e) method throws an 'IllegalStateException' if the addition operation get failed whereas, offerLast(e) method returns 'false' and returns 'true' on successful operation.

Parameter description

e : It takes an element what do you want to insert at the last into a deque.

Example of offerLast(E e) method

In this example we will show you how does an offerLast(E e) method work in BlockingDeque interface. This example will help you to understand how can a specified element be added to the last position into the deque. Through this example we will also show you what does it return when a specified element is added successfully and when this operation get failed.

Example :

package devmanuals.com;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
class OfferLast implements Runnable {
  BlockingDeque<Integer> bdq;
  public OfferLast(BlockingDeque<Integer> bdq) {
    this.bdq = bdq;
  }
  public void run() {
    System.out.println("Elements of deque1 are : ");
    int i;
    boolean bol = false;
    for (i = 0; i < 5; i++)
      bdq.offer(i);
    System.out.println(bdq);
    System.out.println("Size of deque = " + bdq.size());
    try {
      Thread.sleep(1000);
      bol = bdq.offerLast(5);
    catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("The element is added into the deque1 : " + bol);
    System.out.println("Deque after adding element : " + bdq);
    System.out.println("Size of deque after adding element = " + bdq.size());
  }
}
class OfferLast1 implements Runnable {
  BlockingDeque<Integer> bdq1;
  public OfferLast1(BlockingDeque<Integer> bdq1) {
    this.bdq1 = bdq1;
  }
  public void run() {
    System.out.println("Elements of deque2 are : ");
    int i;
    boolean bol = false;
    for (i = 0; i < 5; i++)
      bdq1.offer(i);
    System.out.println(bdq1);
    System.out.println("Size of deque = " + bdq1.size());
    try {
      Thread.sleep(1500);
      bol = bdq1.offerLast(5);
    catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("The element is added into the deque2 : " + bol);
    if (bol == true) {
      System.out.println("Deque2 after adding element : " + bdq1);
      System.out.println("Size of deque after adding element = "+ bdq1.size());
    else {
      System.out.println("The element is not added into the deque2.");
      System.out.println("Elements and Size of deque2 is still \n remains same i.e. size= "+ bdq1.size() " and elements= " + bdq1);
    }
  }
}
public class BdqOfferLast {
  public static void main(String[] args) {
    BlockingDeque<Integer> bdq = new LinkedBlockingDeque<Integer>();
    BlockingDeque<Integer> bdq1 = new LinkedBlockingDeque<Integer>(5);
    Runnable a = new OfferLast(bdq);
    Runnable b = new OfferLast1(bdq1);
    new Thread(a).start();
    try {
      Thread.sleep(300);
    catch (InterruptedException e) {
      e.printStackTrace();
    }
    new Thread(b).start();
    try {
      Thread.sleep(1000);
    catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}

Output :

Elements of deque1 are :

[0, 1, 2, 3, 4]

Size of deque = 5

Elements of deque2 are :

[0, 1, 2, 3, 4]

Size of deque = 5

The element is added into the deque1 : true

Deque after adding element : [0, 1, 2, 3, 4, 5]

Size of deque after adding element = 6

The element is added into the deque2 : false

The element is not added into the deque2.

Elements and Size of deque2 is still

remains same i.e. size= 5 and elements= [0, 1, 2, 3, 4]

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics