Retrieves, but does not remove, the head of this queue. This method differs from peek()
only
in that it throws an exception if this queue is empty.
In case if the queue is empty:
package com.logicbig.example.arrayblockingqueue;
import java.util.concurrent.ArrayBlockingQueue;
public class ElementExample2 {
public static void main(String... args) {
ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(3);
System.out.println("queue: " + q);
Integer element = q.element();
}
}
Output
Caused by: java.util.NoSuchElementException
at java.base/java.util.AbstractQueue.element(AbstractQueue.java:136)
at com.logicbig.example.arrayblockingqueue.ElementExample2.main(ElementExample2.java:16)
... 6 more