[Switch] Analysis of SynchronousQueue, LinkedBlockingQueue, ArrayBlockingQueue, and linkedblockingqueue
Address: http://blog.csdn.net/mn11201117/article/details/8671497
SynchronousQueue
SynchronousQueue is unbounded and a non-buffered waiting Queue. However, due to its own characteristics, after an element is added, you must wait for other threads to remove it before adding it; synchronousQueue can be considered as a blocking queue with a cache value of 1, but the isEmpty () method always returns true, and the remainingCapacity () method always returns 0, remove () and removeAll () the returned value is false forever, The iterator () method returns null forever, And the peek () method returns null forever.
There are two different ways to declare a SynchronousQueue, with different behaviors between them. The difference between the fair mode and the non-fair mode: If the fair mode is adopted: SynchronousQueue adopts a fair lock and works with a FIFO queue to block redundant producers and consumers, thereby establishing an overall fair strategy; however, in the non-fair mode (SynchronousQueue default): SynchronousQueue adopts an unfair lock and uses a LIFO queue to manage redundant producers and consumers. In the latter mode, if there is a gap between the processing speed of the producer and the consumer, it is easy to see hunger and thirst, and data of some producers or consumers will never be processed.
LinkedBlockingQueue
LinkedBlockingQueue is an unbounded cache waiting queue.
The linked list-based blocking queue maintains a Data Buffer Queue internally (this queue is composed of a linked list ). When a producer puts data into the queue, the queue obtains data from the producer and caches the data in the queue. The Producer returns the data immediately; the producer queue is blocked only when the queue buffer reaches the maximum cache capacity (blockingqueue can be specified by the constructor) until the consumer consumes a portion of data from the queue, the producer thread is awakened, and the processing on the consumer side is based on the same principle.
The reason why LinkedBlockingQueue can efficiently process concurrent data is that it also uses independent locks for the producer and consumer to control data synchronization, this also means that in the case of high concurrency, producers and consumers can operate data in the queue in parallel to improve the concurrency performance of the entire queue.
ArrayListBlockingQueue
ArrayListBlockingQueue is a bounded cache waiting queue.
An array-based blocking queue, similar to the LinkedBlockingQueue, maintains a fixed-length Data Buffer Queue (this queue is composed of arrays ). ArrayBlockingQueue also stores two integer variables, which respectively identify the position of the queue's header and tail in the array.
ArrayBlockingQueue shares the same lock object in both the producer's data and the consumer's data, which means that the two cannot run in parallel. This is especially different from the LinkedBlockingQueue. According to the implementation principle, arrayBlockingQueue can completely adopt the separation lock, so as to achieve completely parallel operation of producer and consumer operations. Doug Lea did not do this because ArrayBlockingQueue's Data Writing and acquisition operations are lightweight enough to introduce an independent lock mechanism, which not only brings extra complexity to the code, it has no performance advantage. An obvious difference between ArrayBlockingQueue and LinkedBlockingQueue is that the former will not generate or destroy any additional object instance when inserting or deleting elements, while the latter will generate an additional Node object. In a system that requires efficient and concurrent processing of large volumes of data over a long period of time, there are still some differences in GC impact.
ArrayBlockingQueue and javasblockingqueue are two of the most common and commonly used blocking queues. Generally, they are sufficient to handle producer and consumer issues between multiple threads.