英文:
Memory consumption in arrayBlockingQueue and linkedBlockingQueue
问题
以下是翻译好的内容:
假设我将 arrayBlockingQueue
和 linkedBlockingQueue
的容量都设置为 100。我只在每个队列中添加了 10 个元素。
即使有 90 个空元素,array
是否会保持完整的容量?我的意思是它会有 10 个元素和 90 个 null 值吗?
另外,linked
在这种情况下会如何表现?它会有 10 个还是 100 个节点?会有 90 个“空值节点”吗?
有人可以解释一下这个问题吗?它们在这种情况下会如何表现?
英文:
Say I make capacity of arrayBlockingQueue
and linkedBlockingQueue
both to 100. I add only 10 elements in each of them.
Will array
hold full capacity even though 90 elements are empty? I mean would it have 10 elements and 90 nulls?
Another thing, how would linked
behave in this case? Would it have 10 or 100 nodes? And would there be 90 'null value nodes'?
Can someone explain me this? How do they behave in this case?
答案1
得分: 3
在ArrayBlockingQueue
的情况下,它将创建一个大小为100的数组。其中将包含您插入的10个元素,其余部分将是null
。
在LinkedBlockingQueue
的情况下,只会创建10个节点 - 这里的容量被用作限制,以创建所谓的有界队列,其具有最大条目限制(在多线程环境中,这可能很方便,以免出现内存不足的情况)。
英文:
In case of ArrayBlockingQueue
it will create an array of size 100. It will contain 10 elements that you inserted and the rest will be null
.
In case of LinkedBlockingQueue
only 10 nodes will be created - the capacity here is used as a limitation to create so called bounded queue with maximum entries limit (in multithreaded environment it might be handy to not get out of memory).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论