What is the Difference between ArrayBlockingQueue and LinkedBlockingQueue

前端 未结 4 1324
长发绾君心
长发绾君心 2021-01-30 13:22
  1. What scenarios is it better to use an ArrayBlockingQueue and when is it better to use a LinkedBlockingQueue?
  2. If LinkedBlockingQueue default capacity is equal to M
4条回答
  •  萌比男神i
    2021-01-30 13:56

    Adding an element to ArrayBlockingQueue is supposed to be faster since it means only setting a reference to an element of the backing Object array, while adding an element to LinkedBlockingQueue means creating a Node and setting its item, prev and next fields. Besides, when we remove an element from LinkedBlockingQueue the removed Node becomes garbage which may influence app's performance.

    As for memory consumption ArrayBlockingQueue always holds an Object array with full capacity even when empty. On the other hand one element in LinkedBlockingQueue is a Node with an Object with 3 Object fields.

提交回复
热议问题