Java LinkedBlockingDeque offerFirst() Method

The offerFirst () method of LinkedBlockingDeque class adds the defined elements at the front of this deque.

Syntax:

Parameters:

e - It is the element to be added.

timeout - It is the time till the method has to wait before giving up, in units of unit.

unit - It is the TimeUnit that decides how to interpret the timeout parameter.

Specified By:

The offerFirst() method of LinkedBlockingDeque class is specified by:

  1. offerFirst() method in interface Deque<E>.
  2. offerFirst() method in interface BlockingDeque<E>.

Return Value:

The offerFirst() method returns true if the defined element is added to this deque, else returns false.

Throws:

The offerFirst() method throws:

  1. NullPointerException - If the element defined holds null value.
  2. InterruptedException - If method call is interrupted during wait.

Example 1

Test it Now

Output:

[1, 2, 3, 4, 5]
After inserting 10 at first position
[10, 1, 2, 3, 4, 5]

Example 2

Test it Now

Output:

1. Rohit
2. Ramesh
3. Janak
4. Vishal
5. Yatin
After adding string :
1. Yogesh
2. Rohit
3. Ramesh
4. Janak
5. Vishal
6. Yatin

Example 3

Test it Now

Output:

Exception in thread "main" java.lang.IllegalStateException: Deque full
	at java.util.concurrent.LinkedBlockingDeque.addFirst(Unknown Source)
	at mypack1.LinkedBlockingDequeOfferFirstExample3.main(LinkedBlockingDequeOfferFirstExample3.java:13)





Latest Courses