Mostly this method is preferred to use when we have a limited capacity. The element is to be inserted into the underlying deque only without breaking the capacity limitation of deque. The addFirst(e) method is different from this method only by the addFirst(e) method diplays an 'IllegalStateException' if the addition operation get failed whereas, offerFirst(e) method returns 'false' and 'true' on successful operation.
offerFirst(E e) method of BlockingDeque Interface in Java.
In this section we will discuss how can offerFirst(E e) method be implemented in BlockingDeque interface in java.
Syntax
boolean offerFirst(E e)
This method adds the specified element at the first position into the deque and returns 'true' if currently space is available otherwise, returns 'false'.
Mostly this method is preferred to use when we have a limited capacity. The element is to be inserted into the underlying deque only without breaking the capacity limitation of deque. The addFirst(e) method is different from this method only by the addFirst(e) method diplays an 'IllegalStateException' if the addition operation get failed whereas, offerFirst(e) method returns 'false' and 'true' on successful operation.
Parameter description
e : It takes an element what do you want to insert at the front into a deque.
Example of offerFirst(E e) method
In this example we will show you how does an offerFirst(E e) method work in BlockingDeque interface. This example will help you to understand how can a specified element be added at the first 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 OfferFirst implements Runnable { BlockingDeque<Integer> bdq; public OfferFirst(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.offerFirst(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 OfferFirst1 implements Runnable { BlockingDeque<Integer> bdq1; public OfferFirst1(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.offerFirst(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 BdqOfferFirst { public static void main(String[] args) { BlockingDeque<Integer> bdq = new LinkedBlockingDeque<Integer>(); BlockingDeque<Integer> bdq1 = new LinkedBlockingDeque<Integer>(4); Runnable a = new OfferFirst(bdq); Runnable b = new OfferFirst1(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] Size of deque = 4 The element is added into the deque1 : true Deque after adding element : [5, 0, 1, 2, 3, 4] 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= 4 and elements= [0, 1, 2, 3] |
[ 0 ] Comments