Engineering Full Stack Apps with Java and JavaScript
A Queue is a first in first out (FIFO) data structure, as we have already seen.
BlockingQueue is a queue that additionally supports operations that wait for the queue to become non-empty when retrieving an element, and wait for space to become available in the queue when storing an element.
BlockingQueue methods come in four forms:
those that throws an exception,
those that returns a special value (either null or false, depending on the operation),
those that blocks the current thread indefinitely until the operation can succeed, and
those that blocks for only a given maximum time limit before giving up.
There are also other methods that does no fall to these categories. Look into the documentation (given in reference) for the complete list.
boolean add(e)
Inserts the specified element into this queue
returning true upon success and
throwing an IllegalStateException if no space is currently available.
When using a capacity-restricted queue, it is generally preferable to use offer.
E remove(Object o)
Removes a single instance of the specified element from this queue.
Returns true if this queue contained the specified element
May throw ClassCastException or NullPointerException
E element()
Retrieves, but does not remove, the head of this queue.
Throws NoSuchElementException, if this queue is empty.
differs from peek only in that it throws an exception if this queue is empty.
boolean offer(E e)
Inserts the specified element into this queue
return
true upon success
false if no space is currently available.
When using a capacity-restricted queue, this method is generally preferable to add(E)
E poll()
Retrieves and removes the head of this queue,
Returns null if this queue is empty.
E peek()
Retrieves, but does not remove, the head of this queue.
Returns null if this queue is empty.
void put(e)
Inserts the specified element into this queue, waiting if necessary for space to become available.
May throw InterruptedException, ClassCastException, NullPointerException or IllegalArgumentException.
E take()
Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.
May throw InterruptedException.
boolean offer(E e, long timeout, TimeUnit unit)
Inserts the specified element into this queue, waiting up to the specified wait time if necessary for space to become available.
The unit parameter is a TimeUnit determining how to interpret the timeout parameter.
Returns true if successful, or false if the specified waiting time elapses before space is available.
May throw InterruptedException, ClassCastException, NullPointerException or IllegalArgumentException.
E poll(long timeout, TimeUnit unit)
Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available.
Returns the head of this queue, or null if the specified waiting time elapses before an element is available.
May throw InterruptedException.
Other important methods of BlockingQueue interface include:
boolean contains(Object o)
int drainTo(Collection<? super E> c)
Removes all available elements from this queue and adds them to the given collection.
Returns the number of elements transferred.
int drainTo(Collection<? super E> c, int maxElements)
Removes at most the given number of available elements from this queue and adds them to the given collection.
Returns the number of elements transferred.
int remainingCapacity()
Returns the number of additional elements that this queue can ideally (in the absence of memory or resource constraints) accept without blocking, or Integer.MAX_VALUE if there is no intrinsic limit.
Following classes implement the BlockingQueue interface:
ArrayBlockingQueue
DelayQueue
LinkedBlockingDeque
LinkedBlockingQueue
LinkedTransferQueue
PriorityBlockingQueue
SynchronousQueue
https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/BlockingQ...
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ArrayBloc...
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/DelayQueu...
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlo...
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlo...
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedTra...
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityB...
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Synchrono...