In this method a specified element is to be added at the head without breaking the capacity limitation of deque if an existing deque is restricted with the capacity limitation then the element can not be pushed by this method. This method is equivalent to the addFirst(e) method.
push(e) method of BlockingDeque Interface in Java.
In this section we will discuss how can push(e) method be implemented in BlockingDeque interface in java.
Syntax
void push(E e)
This method adds an element onto the stack represented by the underlying deque.
In this method a specified element is to be added at the head without breaking the capacity limitation of deque if an existing deque is restricted with the capacity limitation then the element can not be pushed by this method. This method is equivalent to the addFirst(e) method.
Parameter description
This method does not return any value.
e : It takes an element what do you want to push into the underlying deque.
Example of push() method
In this example we will show how does push(E e) method work in BlockingDeque interface. This example will help you to understand how you can add a specified element at head position into underlying deque.
Example :
package devmanuals.com; import java.util.concurrent.BlockingDeque; import java.util.concurrent.LinkedBlockingDeque; class Push implements Runnable { String name; BlockingDeque<Integer> bdq; public Push(String name, BlockingDeque<Integer> bdq) { this.name = name; this.dq = 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 { System.out.println(i); Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } // Here the implementation of this method will insert a new element into // the deque. dq.push(11); System.out.println("After inserting the new element \n at first position deque = "+ bdq); } } class Push1 implements Runnable { String name; BlockingDeque<Integer> bdq; public Push1(String name, BlockingDeque<Integer> bdq) { this.name = name; this.bdq = bdq; } public void run() { try { Thread.sleep(1500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Therfore deque = " + bdq); System.out.println("Size of deque = " + bdq.size()); int j = bdq.pollFirst(); System.out.println("After removing the first element (" + j+ "), the remaining \n elements of deque = " + bdq+ " and size = " + bdq.size()); } } public class BdqPush { public static void main(String[] args) { BlockingDeque<Integer> bdq = new LinkedBlockingDeque<Integer>(5); Runnable a = new Push("A", bdq); Runnable b = new Push1("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 first element (1), the remaining elements of deque = [2, 3, 4, 5] and size = 4 After inserting the new element at first position deque = [11, 2, 3, 4, 5] |
[ 0 ] Comments