The implementation of locks in the queue is different
Arrayblockingqueue in the lock is not separated, that is, production and consumption is the same lock;
Linkedblockingqueue in the lock is separate, that is, production is putlock, consumption is Takelock
2. Different operation during production or consumption
Arrayblockingqueue is based on an array, when it is produced and consumed, it is inserted or removed directly from the enumeration object without generating or destroying any additional object instances;
Linkedblockingqueue is based on a linked list, when production and consumption, the need to convert the enumeration object to node<e> for insertion or removal, will generate an additional node object, which in a long period of time requires efficient and concurrent processing of large quantities of data in the system, There are some differences in the effect of GC.
3. The queue size is initialized differently
Arrayblockingqueue is bounded, the size of the queue must be specified;
The Linkedblockingqueue is unbounded and can not specify the size of the queue, but the default is Integer.max_value. Of course, you can also specify the queue size to become bounded.
Attention:
- When using Linkedblockingqueue, a memory overflow is possible when the default size is used and the production speed is greater than the consumption speed.
- When using Arrayblockingqueue and Linkedblockingqueue to queue 1 million simple characters respectively,
Linkedblockingqueue consumption is about 10 times times the consumption of Arrayblockingqueue,
That is, Linkedblockingqueue consumes about 1500 milliseconds, while Arrayblockingqueue only takes about 150 milliseconds.
- By implementing the principle of analysis, Arrayblockingqueue can completely adopt the separation lock, so that the producer and consumer operation of the full parallel operation. Doug Lea did not do this, perhaps because Arrayblockingqueue's data write and fetch operations are lightweight enough to introduce an independent locking mechanism that, in addition to adding additional complexity to the code, is not a good performance benefit.
The difference between Arrayblockingqueue and Linkedblockingqueue