Close

Java Collections - ArrayBlockingQueue.offer() Examples

Java Collections Java Java API 


Class:

java.util.concurrent.ArrayBlockingQueue

java.lang.Objectjava.lang.Objectjava.util.AbstractCollectionjava.util.AbstractCollectionjava.util.CollectionCollectionjava.util.AbstractQueuejava.util.AbstractQueuejava.util.QueueQueuejava.util.concurrent.ArrayBlockingQueuejava.util.concurrent.ArrayBlockingQueuejava.util.concurrent.BlockingQueueBlockingQueuejava.io.SerializableSerializableLogicBig

Methods:

public boolean offer(E e)

Inserts the specified element at the tail of this queue if it is possible to do so immediately without exceeding the queue's capacity, returning true upon success and false if this queue is full.

public boolean offer(E e,
                     long timeout,
                     TimeUnit unit)
              throws InterruptedException

Inserts the specified element at the tail of this queue, waiting up to the specified wait time for space to become available if the queue is full.


Examples


package com.logicbig.example.arrayblockingqueue;

import java.util.concurrent.ArrayBlockingQueue;

public class OfferExample {

public static void main(String... args) {
ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(3);
for (int i = 0; i < 5; i++) {
boolean b = q.offer(i);
System.out.println(b);
}
}
}

Output

true
true
true
false
false




add() method will throw an exception if Queue is full:

package com.logicbig.example.arrayblockingqueue;

import java.util.concurrent.ArrayBlockingQueue;

public class OfferExample2 {

public static void main(String... args) {
ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(3);
for (int i = 0; i < 5; i++) {
try {
boolean b = q.add(i);
System.out.println(b);
} catch (Exception e) {
System.out.println(e);
}
}
}
}

Output

true
true
true
java.lang.IllegalStateException: Queue full
java.lang.IllegalStateException: Queue full




package com.logicbig.example.arrayblockingqueue;

import java.time.LocalTime;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

public class OfferExample3 {

public static void main(String... args) throws InterruptedException {
ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(3);
for (int i = 0; i < 5; i++) {
boolean b = q.offer(i, 1, TimeUnit.SECONDS);
System.out.println("offer succeeded: " + b);
System.out.println("time: " + LocalTime.now());
System.out.println("queue: " + q);
System.out.println("remaining capacity: " + q.remainingCapacity());
System.out.println("---");
}
}
}

Output

offer succeeded: true
time: 14:34:04.314434
queue: [0]
remaining capacity: 2
---
offer succeeded: true
time: 14:34:04.324428900
queue: [0, 1]
remaining capacity: 1
---
offer succeeded: true
time: 14:34:04.325428200
queue: [0, 1, 2]
remaining capacity: 0
---
offer succeeded: false
time: 14:34:05.327652
queue: [0, 1, 2]
remaining capacity: 0
---
offer succeeded: false
time: 14:34:06.328033300
queue: [0, 1, 2]
remaining capacity: 0
---




See Also