获取ArrayBlockingQueue的容量

huangapple go评论72阅读模式
英文:

Get the capacity of an ArrayBlockingQueue

问题

我需要将ArrayBlockingQueue的容量缩减1个。

我认为应该没有比这更容易的方法:

  1. 获取当前队列的容量。
  2. 创建一个新队列,容量为capacity - 1
  3. 使用BlockingQueue.drainTo函数将旧队列中的任何元素移动到新队列中。
  4. 余下的时间休息一下...

然而,我对步骤1感到非常困扰。出于某种不可思议的原因,ArrayBlockingQueue没有公开容量。我想不出这个信息为什么要私有化,它是一个_有界_队列,并提供了getRemainingCapacity方法,为什么我不能获取容量呢?

我不能使用size() + getRemainingCapacity(),因为这是一个并发队列,总有可能在调用sizegetRemainingCapacity之间有某些操作改变队列。

我想我不是第一个需要ArrayBlockingQueue容量的人。除了扩展整个类以添加getCapacity方法,或者创建一个单独的结构来映射我的队列容量之外,是否有一个简单的解决方案?

或者可能有一个合理的原因,不应该知道容量?(除了疏忽之外)

**更多背景信息:**我的多线程程序使用许多队列,它调用BlockingQueue.getRemainingCapacity函数来知道队列何时已满,然后从那里进行处理。在一个罕见的角落情况下,程序发现队列容量在填充过程中过大了一个元素。我需要将队列的大小减小1,否则它永远不会填满,也不会被处理。

英文:

I need to shrink an ArrayBlockingQueue capacity by 1.

I was thinking nothing should be easier:

  1. Get the capacity of the current queue.
  2. Create a new queue with capacity - 1
  3. Use the BlockingQueue.drainTo function to move over any elements in the old queue to the new queue.
  4. 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(&quot;serial&quot;)
public class InformativeBlockingQueue&lt;E&gt; extends ArrayBlockingQueue&lt;E&gt;
{
    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.

huangapple
  • 本文由 发表于 2020年8月9日 06:23:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63320766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定