Atitit. Java Queue Queue System and custom database-based queuing summary o7t
1. Blocking queues and non-blocking queues 1
2. Java.util.Queue interface, 1
3. Concurrentlinkedqueue 2
4. Blockingqueue Blocking 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. Custom queue implement Queue interface 4
7. Reference 4
1. Blocking queues and non-blocking 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-blocking queues, where a typical example of a blocking queue is blockingqueue, a typical example of a nonblocking queue is concurrentlinkedqueue, In practical application, we should choose the blocking queue or non-blocking queue according to the actual need.
synchronization is blocking mode, asynchronous non-blocking 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.
To avoid collection's add () and remove () methods as much as possible, use the offer () to add elements and use poll () to get and move out of the element. They are excellent
Points are determined by the return value, and the Add () and remove () methods throw exceptions when they fail. If you want to use the front end without moving the element, use the
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. BlockingqueueBlocking Queues
Blockingqueue not only realized the basic functions of a complete queue, but also managed to automatically wait for wake-up between multiple lines in a multithreaded environment, allowing programmers to ignore these details and focus on more advanced features.
Blocking implementations are usually implemented with a lock ...
Common Blockingqueue
After understanding the basic functions of blockingqueue, let's take a look at the Blockingqueue family members.
Offer (e)
First, look at the common methods provided by Blockingqueue: |
may report exception |
Return Boolean |
may block |
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 the function of each method can be clearly seen. What I want to say is: the Add (e) Remove () element method does not block the thread. The illegalstateexception exception is thrown when the constraint is not met. For example: When the queue is filled with elements, then add (e) is called, and an exception is thrown.
· Offer (e) the poll () Peek () method does not block the thread and does not throw an exception. For example: When the queue is filled with an element and then call offer (e), the element is not inserted and the function returns FALSE.
· To implement blocking, you need to call the put (e) Take () method . When a constraint is not met, the thread is blocked.
Blockingqueue members Detailed Introduction
4.1.1. ArrayBlockingQueue4.2.2. Linkedblockingqueue
Linked list-based blocking queues
4.3.3. Delayqueue
The element in the Delayqueue can be fetched from the queue only if its specified delay time is reached
4.4.4. Priorityblockingqueue
Priority-based blocking queues (priority judgments are determined by the Compator objects passed into the constructor), but it is important to note that Priorityblockingqueue does not block data producers, but only the consumers who block the data when there is no consumable data. It is therefore important to pay particular attention to the fact that the producer produces data at a speed that is not faster than the consumer's consumption of data, or that it will eventually deplete all available heap memory space for a long time. 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 a double-ended queue
Arrayblockingqueue bidirectional concurrent Blocking queue
Linkedblockingqueue FIFO queue
Concurrentlinkedqueue non-boundary thread secure queue based on link node
Priorityblockingqueue unbounded blocking queue with priority
There are many more, you can see Abstractqueue, deque have what implementation class.
6. Custom Queue Implementation queuesInterface
Implement the Add, remove (obj) method ...
and high-volume operation methods ... addbatch, Peekbatch,
7. Reference
Java thread queue blockingqueue usage -shwenwen-itpub blog . htm
Synchronousqueue Implementation principle of synchronous queue in Java and contract -ifeve.com.htm
Java multithreading summary thread-safe queue queue - fire kapok log - NetEase blog .htm