Atitit. Java Queue Queue system and its own definition of the database-based queue summary o7t

Source: Internet
Author: User

Atitit. Java Queue Queue System and its own definition of the database-based queue summary o7t

1. Block queue and non-clogging queue 1

2. Java.util.Queue interface. 1

3. Concurrentlinkedqueue 2

4. Blockingqueue Jam Queue 2

4.1.1. Arrayblockingqueue 3

4.2.2. Linkedblockingqueue 3

4.3.3. Delayqueue 3

4.4.4. Priorityblockingqueue 3

4.5. Synchronousqueue 3

5. Linkedblockingdeque is blocking the double-ended queue 3

6. Define a queue to implement the Queues interface 4

7. References 4

1. Blocking queues and non-clogging queues

The preferred data structure for most production consumption models is the queue.

Java provides a thread-safe queue that can be divided into blocking queues and non-clogging queues, where the typical example of a blocked queue is blockingqueue, the typical sample of a non-clogging queue is concurrentlinkedqueue, In practical application, we need to choose the blocking queue or the non-clogging queue according to the actual need.

synchronization is clogging mode. Asynchronous non-clogging mode

Author:: Old Wow's paw Attilax Ayron. Email:[email protected]

Reprint please indicate source: Http://blog.csdn.net/attilax

2. Java.util.Queueinterface,

A new Java.util.Queue interface has been added to the JAVA5.

Common operations used to support queues.

The interface extends the Java.util.Collection interface.
Use the queue to avoid the add () and remove () methods of the collection as much as possible. Instead, use an offer () to add elements and use poll () to get and move out of the element. They are excellent
The point is whether the return value can infer success or not, and the Add () and remove () methods throw exceptions when they fail. Suppose you want to use the front end without moving the element out. Use
Element () or peek () method.
It is worth noting that the LinkedList class implements the queue interface, so we can use LinkedList as a queue.

3. ConcurrentLinkedQueue4. BlockingqueueBlock Queue

Blockingqueue not only realizes the basic functions of a complete queue. At the same time in the multi-threaded environment, he also actively manages the multi-line between the active waiting for the wake-up function, so that the program apes can ignore these details, focus on more advanced features.

Plug implementations are usually implemented with a lock ...

Common Blockingqueue


After understanding the basic functions of blockingqueue. Let's take a look at the general membership of the Blockingqueue family.

Offer (e)

First, look at the regular usage provided by blockingqueue:  
 

may report exception

Return Boolean

May clog

Set wait time

Queue

Add (e)

put (e)

Offer (E,  timeout, unit)

Outbound

Remove ()

Poll ()

Take ()

PO LL (timeout, unit)

View

Element ()

Peek ()

None

None

·
It is clear from the table above that each method works, and this does not have to say much. What I want to say is that the Add (e) Remove () Element () method does not clog the thread. The illegalstateexception exception is thrown when the constraint is not met. For example, when the queue is filled with elements. Then call Add (E). The exception is thrown.

· Offer (e) the poll () Peek () method does not clog the thread or throw an exception. For example: When the queue is filled with elements, then call the Offer (e). The element is not inserted, and the function returns FALSE.

·  to implement blocking, you need to call the put (e) Take () method .

The thread is blocked when the constraint is not met.



Blockingqueue members Specific Introduction

4.1.1. ArrayBlockingQueue4.2.2. Linkedblockingqueue

Linked list-based blocking queue

4.3.3. Delayqueue

The element in Delayqueue only has the delay time when it is specified. To get to the element from the queue.

4.4.4. Priorityblockingqueue

Priority-based congestion queuing (priority inference is determined by the Compator object passed into the constructor), but it is important to note that Priorityblockingqueue does not clog the data producer, but only when there is no data to consume. Consumers who clog data. It is therefore important to pay particular attention to the speed at which producers produce data faster than consumers consume data. Otherwise, a long time will eventually drain all the available heap memory space.

When implementing Priorityblockingqueue, the internal control thread synchronizes the lock with a fair lock.

4.5. SynchronousQueue5. LinkedblockingdequeIs blocking the double-ended queue

Arraydeque bidirectional queue
Linkedblockingdeque blocking the double-ended queue
Arrayblockingqueue bidirectional concurrent Blocking queue
Linkedblockingqueue FIFO queue
Concurrentlinkedqueue non-boundary thread secure queue based on link node
Priorityblockingqueue unbounded congestion queue with priority
There are a lot of very many that can look at abstractqueue. What implementation classes are deque.

6. Define queue for yourselfInterface

Implement the Add, remove (obj) method ...

and high-volume operation methods ... addbatch, Peekbatch,

7. References

How to use the thread queue blockingqueue in Java -shwenwen-itpub blog . htm

Synchronousqueue Implementation principle of synchronous queue in Java and contract -ifeve.com.htm

Java Multithreading Summary thread-safe queue - log of Fire Kapok - netease blog . htm

Atitit. Java Queue Queue system and its own definition of the database-based queue summary o7t

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.