offer(e) method of BlockingDeque Interface in java.

offer(e) method of BlockingDeque Interface in java.


Posted in : Core Java Posted on : February 23, 2011 at 4:25 PM Comments : [ 0 ]

This method is mostly preferred for use when we have a capacity-limitation with deque. It can inserts an element only when the space is available but, can not inserts by breaking the capacity limitation of the deque. The add(E e) method is different from this method only by their behavior, the offer(E e) method returns 'false' if insertion of an element in deque gets fail whereas add(E e) method throws an 'IllegalStateException' exception. This method is equivalent to offerLast(e) method.

offer(e) method of BlockingDeque Interface in java.

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

Syntax

boolean offer(E e)

This method inserts a specific element at the tail into the deque if currently space is available and returns 'true' on success otherwise, returns 'false'.

This method is mostly preferred for use when we have a capacity-limitation with deque. It can inserts an element only when the space is available but, can not inserts by breaking the capacity limitation of the deque. The add(E e) method is different from this method only by their behavior, the offer(E e) method returns 'false' if insertion of an element in deque gets fail whereas add(E e) method throws an 'IllegalStateException' exception. This method is equivalent to offerLast(e) method.

Parameter description

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

Example of offer(E e) method 

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

Example :

package devmanuals.com;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
class Offer implements Runnable {
  BlockingDeque<Integer> bdq;
  public Offer(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.offer(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 Offer1 implements Runnable {
  BlockingDeque<Integer> bdq1;
  public Offer1(BlockingDeque<Integer> bdq1) {
    this.bdq1 = bdq1;
  }
  public void run() {
    System.out.println("Elements of deque2 are : ");
    int i;
    boolean bol = false;
    for (i = 1; i <= 5; i++)
      bdq1.offer(i);
    System.out.println(bdq1);
    System.out.println("Size of deque = " + bdq1.size());
    try {
      Thread.sleep(1500);
      bol = bdq1.offer(6);
    catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("The element is added into the deque2 : " + bol);
    if (bol == true) {
      System.out.println("Deque 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 BdqOffer {
  public static void main(String[] args) {
    BlockingDeque<Integer> bdq = new LinkedBlockingDeque<Integer>();
    BlockingDeque<Integer> bdq1 = new LinkedBlockingDeque<Integer>(5);
    Runnable a = new Offer(bdq);
    Runnable b = new Offer1(bdq1);
    new Thread(a).start();
    try {
      Thread.sleep(1000);
    catch (InterruptedException e) {
      e.printStackTrace();
    }
    new Thread(b).start();
    try {
      Thread.sleep(300);
    catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}

Output :

Elements of deque1 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

Elements of deque2 are :

[1, 2, 3, 4, 5]

Size of deque = 5

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= [1, 2, 3, 4, 5]

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics