Getting Familiar with the BlockingQueue Interface

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:

  1. those that throws an exception,

  2. those that returns a special value (either null or false, depending on the operation),

  3. those that blocks the current thread indefinitely until the operation can succeed, and

  4. 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.

 

BlockingQueue methods that throws an Exception

  • 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.

 

BlockingQueue methods that returns a special value

  • 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.

 

BlockingQueue methods that blocks the current thread

  • 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.

 

BlockingQueue methods that blocks for only a given maximum time limit

  • 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

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.

 

Implementing classes

Following classes implement the BlockingQueue interface:

  • ArrayBlockingQueue

  • DelayQueue

  • LinkedBlockingDeque

  • LinkedBlockingQueue

  • LinkedTransferQueue

  • PriorityBlockingQueue

  • SynchronousQueue

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!