Java ArrayBlockingQueue offer() Method

The offer() method of Java ArrayBlockingQueue class adds the defined element at the tail of this queue, only if the queue is not full or it waits till the specified wait time for space to get free, if the queue is full.

Syntax:

Parameters:

e - This is the element to be added.

timeout - This is the time to wait before giving up(in units of unit).

unit - This is the TimeUnit deciding how to represent the timeout parameter.

Specified By:

The offer() method of ArrayBlockingQueue class is specified by:

  1. offer() method in interface Queue<E>.
  2. offer() method in interface BlockingQueue<E>.

Throws:

The offer() method throws:

  1. NullPointerException - if the element defined holds null value.
  2. InterruptedException - if method is disrupted while waiting.

Return Value:

The offer() method returns true on successful insertion, otherwise returns false.

Or

The offer() method returns true on successful insertion, or false if the assigned waiting time passes before space is available.

Example 1

Output:

567
56
5671
5167
5670

Example 2

Output:

Reema
Rahul
Rita
Ramesh

Example 3

Output:

Reema       Panda     [email protected]
Geetanjali  Sharma    [email protected]
Vineet      Tanjea    [email protected]
Himanshu    Bhardwaj  [email protected]