Blocking queue --- BlockingQueue

BlockingQueue

1, the structure BlockingQueue class Here Insert Picture Description
2, inheritance

public interface BlockingQueue<E> extends Queue<E> 

BlockingQueue inherited from the Queue interface, on this basis, an increase of blocking operation, can support two additional operations:

  • Supports blocking added: Add element will be stuck in the queue is full, you can add up to a queue before returning success (put ());
  • Supports blocking removal method: the queue is empty, remove elements of the thread will be blocked until the queue has elements can successfully return (take ()) is deleted;

3, application scenarios : blocking queue commonly used in the producer-consumer scenario, the producer is the process of adding elements to the queue, the consumer is the process of taking elements from the queue, blocking queue is used to store elements producer, consumer elements are used to obtain the container;

4, the method provided

The method / approach Throw an exception Returns the special value Clog Timeout exit
insert add(e) offer(e) put(e) offer(e, time, unit)
Remove remove() poll() take() poll(time, unit)
an examination element() peek() unavailable unavailable

1) throw an exception:

  • When the queue is full, if you go down the queue to insert elements, throws IllegalStateException ( "Queue
    Full") anomaly;
  • When the queue is empty when retrieving an element from the queue NoSuchElementException will throw an exception;

2) returns the special value:

  • When inserted into the queue element, the element is inserted successfully returns, returns true if successful;
  • If the method is to remove, an element is removed from the queue, if not then return null;

3) obstruction:

  • When the blocking queue is full, if the producer thread to put queue element, the queue will block until the producer thread until the queue is available or respond to abort;
  • When the queue is empty, if the consumer thread take elements from the queue, the queue will be blocked live consumer thread until the queue is not empty;

4) Exit Timeout:

  • When the blocking queue is full, if the producer thread insert elements into the queue, the queue will block the producer thread for some time, if more than a specified time, the producer thread exits.

Note : If you are unbounded blocking queue , the queue will be full of impossible situations:

  • Use the put or offer methods will never be blocked;
  • When using the offer method, which returns true forever;

5, Java provides seven blocked queue

1) ArrayBlockingQueue: the array of structures bounded blocking queue;
2) a LinkedBlockingQueue: a list structure consisting bounded blocking queue;
. 3) a PriorityBlockingQueue is: Support unbounded blocking queue priority ranking;
. 4) a DelayQueue: using priority queues achieve unbounded blocking queue;
. 5) SynchronousQueue: synchronous blocking queue element is not stored;
. 6) LinkedTransferQueue: a list structure consisting of unbounded blocking queue;
. 7) LinkedBlockingDeque with: a two-way linked list of structures blocking queue;

6, blocking queue using the notification mode to achieve

  Notification mode, that is, when the producers to full queue will block live Producer elements are added, when consumer spending elements of a queue, the queue will inform producers currently available. For example ArrayBlockingQueue using a Condition to achieve.

Guess you like

Origin blog.csdn.net/Daria_/article/details/89501038