Blockingqueue
It implements the queue interface. It is a blockingqueue with one thread putting into it, and another the thread taking from it. One end of production is consumed at one end.
One of the threads will constantly put the task into blockingqueue until it encounters its critical value, but does not allow NULL to be inserted, otherwise it throws nullpointerexception. Another thread is constantly fetching tasks from it.
Methods of Blockingqueue
Blockingqueue has 4 different sets of methods for inserting, removing, and checking the elements in the queue. If the requested operation cannot be performed immediately, the performance of each method is different. These methods are as follows:
|
Throw exception |
specific value |
blocking |
timeout |
| Insert |
Add (o) |
Offer (O) |
Put (o) |
Offer (o, timeout, timeunit) |
| Removed from |
Remove (o) |
Poll (o) |
Take (o) |
Poll (timeout, timeunit) |
| Check |
Element (O) |
Peek (o) |
|
|
Four different sets of behavioral patterns are explained:
- Throw exception : Throws an exception if the attempted operation cannot be executed immediately. illegalstateexception
- Specific value : Returns a specific value (often true/false) if the attempted operation cannot be performed immediately.
- blocking : If the attempted operation cannot be executed immediately, the method call will block until it is able to execute.
- Timeout : If the attempted operation cannot be executed immediately, the method call will block until it can execute, but the wait time will not exceed the given value. Returns a specific value to tell if the operation was successful (typically True/false).
The realization of Blockingqueue
Blockingqueue is an interface, you need to use one of its implementations to use Blockingqueue. The java.util.concurrent has the following implementation of the Blockingqueue interface (Java 6):
- Arrayblockingqueue
- Delayqueue
- Linkedblockingqueue
- Priorityblockingqueue
- Synchronousqueue
Using Arrayblockingqueue to achieve:
//Consumer Public classConsumer implements Runnable {PrivateBlockingqueue<string>queue; PublicConsumer (blockingqueue<string>queue) { This. Queue =queue; } @Override Public voidrun () {Try{System. out. println (Queue.take ()); System. out. println (Queue.take ()); System. out. println (Queue.take ()); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}//producers Public classProducer implements Runnable {PrivateBlockingqueue<string>queue; PublicProducer (blockingqueue<string>queue) { This. Queue =queue; } @Override Public voidrun () {Try{Queue.add ("001"); Thread.Sleep ( +); Queue.add ("002"); Thread.Sleep ( +); Queue.add ("003"); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}//Test Public Static voidMain (string[] args) {Executor Executor=Executors.newcachedthreadpool (); Blockingqueue<String> queue =NewArrayblockingqueue<string> ( -); Executor.execute (NewProducer (queue)); Executor.execute (NewConsumer (queue)); }
The above test is not related to the Consumer/producer execution order. As mentioned above, when taken, it will block.
Array Blocking Queue Arrayblockingqueue
Arrayblockingqueue is a bounded blocking queue whose internal implementation is to place objects in an array. Since it is implemented using arrays, it is necessary to satisfy some of the features of the array, such as the inefficient deletion and the inability to change its size. Arrayblockingqueue uses FIFO to organize the data. At the same time, Arrayblockingqueue provides a kind of fairness strategy.
When public arrayblockingqueue (int capacity, Boolean fair) {} Fair=true, the FIFO policy is met.
Delay Queue Delayqueue
This is an unbounded blocking queue in which each element in the queue has a delay period that can only take if the element's delay period is invalidated. The first element of the team is the element with the longest expiration time. If there is no expiration element, then NULL is returned.
Getdelay (timeunit unit)
Returns the duration of an element's expiration. The element is available only when it is less than or equal to 0.
Chain Blocking Queue Linkedblockingqueue
The Linkedblockingqueue is an optional bounded blocking queue. The policy for handling elements is also FIFO. Satisfies the nature of the linked list. Linkedblockingqueue the blocking queue implemented relative to the array has a higher throughput, but slightly less performance.
Blocking queue with priority Priorityblockingqueue
The Priorityblockingqueue is a unbounded concurrent queue. It uses the same sort rules as the class Java.util.PriorityQueue. You cannot insert a null value into this queue.
All elements inserted into the Priorityblockingqueue must implement the Java.lang.Comparable interface. So the ordering of the elements in this queue depends on your own comparable implementation.
Note that Priorityblockingqueue does not enforce any specific behavior for elements that have an equal priority (compare () = = 0).
Synchronization Queue Synchronousqueue
Synchronousqueue is a special queue that can hold only a single element in its interior. If the queue already has an element, the thread that is trying to insert a new element into the queue will block until another thread is pulling the element out of the queue. Similarly, if the team is listed as empty, the thread attempting to extract an element into the queue will block until another thread inserts a new element into the queue.
Blocking the double-ended queue Blockingdeque
J Ava.util.concurrent the Blockingdeque interface in the packet represents a line Shuo the double-ended queue that puts and extracts the instance. In this section I will show you how to use Blockingdeque.
The Blockingdeque class is a double-ended queue that blocks the thread that attempts to insert an element when it is not able to insert it, and blocks the thread that is trying to extract when the element is not able to be extracted.
The Deque (double-ended queue) is the abbreviation for double Ended queue. Therefore, the double-ended queue is a queue where you can insert or extract elements from either end.
"See" http://blog.csdn.net/defonds/article/details/44021605/
Http://tutorials.jenkov.com/java-concurrency/index.html
Java.util.concurrent Blockingqueue