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

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)