put(e) method of BlockingDeque Interface in Java.

put(e) method of BlockingDeque Interface in Java.


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

To insert an element this method waits (if necessary) until the waiting time that is defined for becoming the space available. This method is similar to the putLast() method.

put(e) method of BlockingDeque Interface in Java.

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

Syntax

void put(E e)

This method adds a specified element at the tail into the deque.

To insert an element this method waits (if necessary) until the waiting time that is defined for becoming the space available. This method is similar to the putLast() method.

Parameter description

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

Example of put() method

In this example we will show you how does put(E e) method work in BlockingDeque interface. This example will help you to understand how can you insert a specified element at the last position into the underlying deque.

Example :

package devmanuals.com;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
class Put implements Runnable {
  String name;
  BlockingDeque<Integer> bdq;
  public Put(String name, BlockingDeque<Integer> bdq) {
    this.name = name;
    this.bdq = bdq;
  }
  public void run() {
    System.out.println(name + " inserted the element into the deque");
    for (int i = 1; i < 6; i++) {
      bdq.offer(i);
      try {
        Thread.sleep(400);
        System.out.println(i);
        }
      catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    System.out.println("Therfore deque = " + bdq);
    System.out.println("Size of deque = " + bdq.size());
    int j = bdq.pollLast();
    System.out.println("After removing the last element (" + j"), the remaining \n elements of deque = " + bdq" and size = " + bdq.size());
  }
}
class Put1 implements Runnable {
  String name;
  BlockingDeque<Integer> bdq;
  public Put1(String name, BlockingDeque<Integer> bdq) {
    this.name = name;
    this.bdq = bdq;
  }
  public void run() {
    try {
      Thread.sleep(1500);
      bdq.put(11);
      System.out
          .println("Inserting a new element \n at last position then deque = "+ bdq + "and size = " + bdq.size());
    catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}
public class BdqPut {
  public static void main(String[] args) {
    BlockingDeque<Integer> bdq = new LinkedBlockingDeque<Integer>(5);
    Runnable a = new Put("A", bdq);
    Runnable b = new Put1("B", bdq);
    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

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

Size of deque = 5

After removing the last element (5), the remaining

elements of deque = [1, 2, 3, 4] and size = 4

Inserting a new element

at last position then deque = [1, 2, 3, 4, 11]and size = 5

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics