Make delicious recipes!

BlockingQueue

A BQ is a Q whose put method blocks if the Q has reached its maximum capacity and its get method blocks if the Q is already empty.

Types of BlockingQueue

Apart from normal synchronized put method, BQ also supports blocking of threads if they try to read the Q when empty or try to write to the Q when its full.

It has following flavors:

  1. ArrayBlockingQueue

  2. LinkedBlockingQueue

  3. PriorityBlockingQueue

  4. DelayQueue: Same as #1 except that it accepts objects implementing Delayed interface only. Delayed mandates implementation of getDelay(TimeUnit) which should return the remaining delay associated with this object. So every object in DelayQueue is available only after the delay specified with that object has passed.

  5. SynchronousQueue: This is a BQ having capacity of only one element. Due to capacity of one element only, it forces single production followed by single consumption.

  6. LinkedTransferQueue: This is same as BQ except that it offers function which can make a producer wait for the consumer to consume it. This is done by adding methods:

    1. void transfer(T obj) : blocks the producer till transfer happens

    2. boolean tryTransfer (T obj) : returns immediately indicating if the transfer happened or not.

    3. boolean tryTransfer (T obj, long timeCount, TimeUnit unit) : same as above but with a time-out.







Like us on Facebook to remain in touch
with the latest in technology and tutorials!


Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Name:
Email: (Your email is not shared with anybody)
Comment:

Facebook comments:

Site Owner: Sachin Goyal