JDK container and concurrency-Queue-Interface

Source: Internet
Author: User

JDK container and concurrency-Queue-Interface
Framework Overview

Interface Introduction

The queue is designed to store elements before processing. On the basis of Collection, the methods for joining, leaving, and accessing the first element of the team are added:

1) the Queue has two sets of methods with the same functions: add, remove, and element are the exception versions thrown by the methods for joining, leaving, and accessing the first element; offer, poll, and peek are the versions with special values returned:

 

(2) offer is often used in bounded queues. When the queue is full, false will be returned for element joining, rather than throwing an exception, because it is generally considered normal;
3) elements can be divided into FIFO queue, LIFO queue, and priority queue according to the order of incoming and outgoing elements. One of the three queues is that the elements returned by remove and poll are the first elements of the queue:

 

 

4) Queue generally does not implement the element-based equals and hashCode methods, because multiple equal elements may exist in the Queue, but their order is different, "order" is very special in the queue.

 

BlockingQueue

The queue is commonly known as a blocking queue. It can be deleted until there are elements and added until the queue has capacity space:

1)It is designed to serve as a producer-consumer queue.;

2) BlockingQueue implementation classes are thread-safe, and their methods adopt locks or other forms of concurrency (such as lock-free and wait-free) to achieve atomicity, batch operations such as addAll, ContainsAll, retainAll, and removeAll are not necessarily atomic unless otherwise implemented;

3) There are four different operations:

4) BlockingQueue has internal capacity restrictions. You can use remainingCapacity () to view the restrictions. If not, the value is Integer. MAX_VALUE;

5) the null element is not supported. Adding null will throw NullPointerException. null is used to check whether the poll operation fails;

6) memory consistency principle: the operation of "retrieving or deleting this element" is prior to the operation of "adding an element to BlockingQeque queue" happens-before;

7) for producer-consumer application scenarios, thread security under multiple producer and consumer threads:

 

class Producer implements Runnable {private final BlockingQueue queue;Producer(BlockingQueue q) { queue = q; }public void run() {try {while (true) { queue.put(produce()); }} catch (InterruptedException ex) { ... handle ...}}Object produce() { ... }}class Consumer implements Runnable {private final BlockingQueue queue;Consumer(BlockingQueue q) { queue = q; }public void run() {try {while (true) { consume(queue.take()); }} catch (InterruptedException ex) { ... handle ...}}void consume(Object x) { ... }}class Setup {void main() {BlockingQueue q = new SomeQueueImplementation();Producer p = new Producer(q);Consumer c1 = new Consumer(q);Consumer c2 = new Consumer(q);new Thread(p).start();new Thread(c1).start();new Thread(c2).start();}}

 

Deque

It is also known as a dual-end queue. It supports bounded capacity and unbounded capacity.

Deque does not support the index access method of the List. Although null elements are allowed, do not use them as much as possible. null is used as a special value of the method. element-based equals and hashCode methods are generally not defined, the native method of Object is used.

Method:

Used as the FIFO equivalent method:

Used as the LIFO (Stack) equivalent method:

BlockingDeque

Blocking + Deque:

Memory consistency principle: the operation of "getting or deleting this element" is prior to the operation of "adding an element to BlockingDeque queue" happens-before;

TransferQueue

Blocking + Transfer, Also known as the transmission queue, allows producers to wait for consumers to obtain elements.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.