ArrayBlockingQueue:应该使用来创建Pool吗?

发布于 01-03 15:54 字数 488 浏览 6 评论 0原文

我正在尝试创建一个 Pool 对象来保留旧对象,以便再次使用它们(以避免实例化新对象)。我在谷歌上搜索了ArrayBlockingQueue,有些人用它来创建Pool。但有一个问题我不知道:当对象插入其中时,它是否会重新创建一个新实例。

例如:ArrayBlockingQueuepool = new ArrayBlockingQueue(3);

短时间后: pool = (3,4,5);

pool.take(5); ==> pool = (3,4);
pool.put(6);  ==>pool = (6,3,4);

所以,我想知道 6 是否分配给旧的 Integer 对象(与值 5),还是 Java 创建一个新值并将其值指定为 6?

谢谢 :)

I'm trying to create a Pool object to reserve old objects in case of use them again (to avoid instantiation of new objects). I google that ArrayBlockingQueue and some people use it to create Pool. But there is one question I don't know: does it recreate a new instance when an object insert to it.

For example: ArrayBlockingQueue<Integer> pool = new ArrayBlockingQueue<Integer>(3);

after short time: pool = (3,4,5);

pool.take(5); ==> pool = (3,4);
pool.put(6);  ==>pool = (6,3,4);

So, I wonder is 6 assigned to the old Integer object (with value 5), or does Java create a new one and assign it's value as 6?

thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦境2025-01-10 15:54:50

ArrayBlockingQueue 由参数类型的数组支持。所以在内部它看起来像这样:

E[] items;

并在您的情况下实例化为

Integer[] items;

根据 源码put方法实际上调用了这个insert方法:

private void insert(E x) {
    items[putIndex] = x;
    putIndex = inc(putIndex);
    ++count;
    notEmpty.signal();
}

那么当你调用时会发生什么>pool.put(6)int 6 被装箱到 Integer 对象中并传递给该方法(因为 E 现在是 Integer )。因此可以肯定地说,它确实创建了 Integer 的新实例。

The ArrayBlockingQueue is backed by an array of the parameter type. So internally it would look something like this:

E[] items;

and instantiated in your case as

Integer[] items;

According to the source code of ArrayBlockingQueue, the put method actually calls this insert method:

private void insert(E x) {
    items[putIndex] = x;
    putIndex = inc(putIndex);
    ++count;
    notEmpty.signal();
}

So what happens when you call pool.put(6) is that the int 6 is boxed into an Integer object and passed to the method (since E is now Integer). So it's safe to say that indeed it does create a new instance of Integer.

花想c2025-01-10 15:54:50

看你不应该担心底层的实现,这就是java中“封装”的目的。根据 Oracle 文档,“put”实际上是在尾部“插入”元素。所以我想,“旧物体”没有任何替代品。

See you shouldn't worry about the underlying implementation, that is what is intended in java by "encapsulation". According to the Oracle docs, "put" actually "inserts" the element at the tail. So there isn't any replacement of "old objects", i suppose.

空名2025-01-10 15:54:50

我严重怀疑在这种情况下任何值的替换都是真实的。此外,我不确定此类对象池的自定义实现是否有用,除非您的代码生成并丢弃了大量对象。

更有趣的是,您在问题中没有提及任何有关线程安全或多线程的内容,但您使用了这些标签。您到底想通过这样一个池实现什么目标? ArrayBlockingQueue 旨在作为一种线程安全的集合,其中一个(或多个)线程转储对象,同时一个(或多个)线程删除对象。有很多方法可以提供不同的行为,以防队列中需要对象但没有对象,或者添加对象但队列中没有容量。您应该查看 javadoc 看看它是否真的是你想要的 ArrayBlockingQueue 。

I would seriously doubt that any replacing of values would be actual in this case. Furthermore, I am not sure having a custom implementation of such an object pool would be useful, unless your code generates and trashes an immense number of objects.

What's more interesting is that you do not mention anything about thread safety or multithreading in your question, but you have used these tags. What exactly is it that you want to achieve with such a pool? ArrayBlockingQueue is intended as a thread-safe collection where one (or many) threads dump in objects while one (or many) threads remove objects out. There are quite a few methods to supply different behaviors in case an objects is required from the queue but there is none, or when an objects is added but there is no capacity in the queue. You should check out the javadoc and see if it's really ArrayBlockingQueue you want.

薄情伤2025-01-10 15:54:50

新对象就在这里创建:

pool.put(6);

当您想到自动装箱将其转换为什么时,情况会更明显:

pool.put(new Integer(6));

队列既不创建也不重用这些对象,它存储您提供的对象。

The new object was created right here:

pool.put(6);

It's more obvious when you think what autoboxing is converting that to:

pool.put(new Integer(6));

The queue neither creates nor reuses these objects, it stores the ones you give it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文