英文:
Get the capacity of an ArrayBlockingQueue
问题
我需要将ArrayBlockingQueue的容量缩减1个。
我认为应该没有比这更容易的方法:
- 获取当前队列的容量。
- 创建一个新队列,容量为
capacity - 1
。 - 使用
BlockingQueue.drainTo
函数将旧队列中的任何元素移动到新队列中。 - 余下的时间休息一下...
然而,我对步骤1感到非常困扰。出于某种不可思议的原因,ArrayBlockingQueue没有公开容量。我想不出这个信息为什么要私有化,它是一个_有界_队列,并提供了getRemainingCapacity
方法,为什么我不能获取容量呢?
我不能使用size() + getRemainingCapacity()
,因为这是一个并发队列,总有可能在调用size
和getRemainingCapacity
之间有某些操作改变队列。
我想我不是第一个需要ArrayBlockingQueue容量的人。除了扩展整个类以添加getCapacity
方法,或者创建一个单独的结构来映射我的队列容量之外,是否有一个简单的解决方案?
或者可能有一个合理的原因,不应该知道容量?(除了疏忽之外)
**更多背景信息:**我的多线程程序使用许多队列,它调用BlockingQueue.getRemainingCapacity
函数来知道队列何时已满,然后从那里进行处理。在一个罕见的角落情况下,程序发现队列容量在填充过程中过大了一个元素。我需要将队列的大小减小1,否则它永远不会填满,也不会被处理。
英文:
I need to shrink an ArrayBlockingQueue capacity by 1.
I was thinking nothing should be easier:
- Get the capacity of the current queue.
- Create a new queue with
capacity - 1
- Use the
BlockingQueue.drainTo
function to move over any elements in the old queue to the new queue. - Take the rest of the day off...
However, I am thoroughly bugged at step 1. For some inconceivable reason ArrayBlockingQueue does not expose the capacity. I can think of no reason for this to be private, it is a bounded queue and offers the getRemainingCapacity
method, why can I not get the capacity?
I cannot do size() + getRemainingCapacity()
because this is a concurrent queue, there is always the possibility of something changing the queue in between the size and getRemainingCapacity calls.
Surely I am not the first person to need a capacity for an ArrayBlockingQueue. Short of extending the whole class to add a getCapacity
method, or creating a separate structure to map the capacity of my queues, is there a simple solution for this?
Or maybe an intelligent reason why the capacity should not be known? (aside from an oversight)
More background: My multi-threaded program uses many queues, it calls the BlockingQueue.getRemainingCapacity
function to know when a queue is full and then process it from there. In a rare corner case the program discovers that the queue capacity is 1 element too large partway through filling it up. I need to reduce the size of the queue by 1 otherwise it will never fill up and never get processed.
答案1
得分: 3
以下是您要翻译的代码部分:
现在,我正在使用这个...
import java.util.concurrent.ArrayBlockingQueue;
@SuppressWarnings("serial")
public class InformativeBlockingQueue<E> extends ArrayBlockingQueue<E>
{
private final int visibleCapacity;
public InformativeBlockingQueue(int capacity)
{
super(capacity, false);
this.capacity = capacity;
}
public InformativeBlockingQueue(int capacity, boolean fair)
{
super(capacity, fair);
this.visibleCapacity = capacity;
}
public int getCapacity()
{
return visibleCapacity;
}
}
我觉得获取容量不应该需要扩展。
英文:
For now, I am going with this...
import java.util.concurrent.ArrayBlockingQueue;
@SuppressWarnings("serial")
public class InformativeBlockingQueue<E> extends ArrayBlockingQueue<E>
{
private final int visibleCapacity;
public InformativeBlockingQueue(int capacity)
{
super(capacity, false);
this.capacity = capacity;
}
public InformativeBlockingQueue(int capacity, boolean fair)
{
super(capacity, fair);
this.visibleCapacity = capacity;
}
public int getCapacity()
{
return visibleCapacity;
}
}
I feel like it should not require an extension just to get the capacity though.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论