addLast(E e) method of BlockingDeque Interface in Java.

addLast(E e) method of BlockingDeque Interface in Java.


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

This method is equivalent to the add(E e) method. An element is to be inserted into the underlying deque without breaking the capacity limitation of deque. This

addLast(E e) method of BlockingDeque Interface in Java.

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

Syntax

void addLast(E e)

This method adds the specified element at the last position into the deque if currently space is available.

This method is equivalent to the add(E e) method. An element is to be inserted into the underlying deque without breaking the capacity limitation of deque. This method is slightly different from the add(e) method, as add(e) method returns 'true' on successful addition of an element but the addLast(e) returns no value but, both the methods displays an 'IllegalStateException' exception when adding operation gets failed due to capacity limitation of deque.

Parameter description

e : It takes an element what do you want to add in deque.

Example of addLast(E e) method

In this example we will show you how does addLast(E e) method work in BlockingDeque interface. In the following example we will show how can you add a specified element at the end position of deque, and count the total number of elements of the underlying deque before and after adding the elements.

Example :

package devmanuals.com;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
class AddLast implements Runnable 
{
  BlockingDeque<Integer> bdq;
  public AddLast(BlockingDeque<Integer> bdq
  {
    this.bdq = bdq;
  }
 public void run() 
 {
    System.out.println("Elements of deque are : ");
    int i;
    for(i=1; i<5;i++)
      bdq.add(i);
      System.out.println(bdq);
      System.out.println("Size of deque = "+bdq.size());
    try {
        Thread.sleep(1000);
        bdq.addFirst(0);
      }
      catch (InterruptedException e
      {
        e.printStackTrace();
      }
      System.out.println("Deque after adding element at the first position: "+bdq);
     System.out.println("Size of deque after adding element at the first position = "+bdq.size());
  }
}
public class BdqAddLast {
  public static void main(String[] args) {
    BlockingDeque<Integer> bdq = new LinkedBlockingDeque<Integer>();
    Runnable a = new AddLast(bdq);
    new Thread(a).start();
    try{
      Thread.sleep(2000);
      bdq.addLast(5);
    }
    catch(InterruptedException e){}
    System.out.println("Deque after adding element at the last position: "+bdq);
    System.out.println("Size of deque after adding element at the last position = "+bdq.size());
  }
}

Output :

Elements of deque are :

[1, 2, 3, 4]

Size of deque = 4

Deque after adding element at the first position: [0, 1, 2, 3, 4]

Size of deque after adding element at the first position = 5

Deque after adding element at the last position: [0, 1, 2, 3, 4, 5]

Size of deque after adding element at the last position = 6

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics