在java 7/8中,有没有具有大小限制的linkedtransfer队列的替代方案?

在java 7/8中,有没有具有大小限制的linkedtransfer队列的替代方案?,java,apache-commons,java.util.concurrent,Java,Apache Commons,Java.util.concurrent,为了实现生产者/消费者模式,我使用了 . 检查下面的代码 while (true) { String tmp = new randomString(); if (linkedTransferQueueString.size() < 10000) { linkedTransferQueueString.add(tmp); } } while(true){ 字符串tmp=新的随机字符串(); if(linkedT

为了实现生产者/消费者模式,我使用了 .

检查下面的代码

while (true) {

    String tmp = new randomString();
    if (linkedTransferQueueString.size() < 10000) {
        linkedTransferQueueString.add(tmp);             
        }

}
while(true){
字符串tmp=新的随机字符串();
if(linkedTransferQueueString.size()<10000){
linkedTransferQueueString.add(tmp);
}
}
从文档中,它声明大小为O(n)操作:(。因此,对于添加元素,它应该贯穿整个集合

是否有其他具有大小限制的并发收集队列?
在中找不到任何内容,

是否尝试了
ArrayBlockingQueue

它具有大小限制和并发性

此外,尺寸为O(1)


您是否尝试过
ArrayBlockingQueue

它具有大小限制和并发性

此外,尺寸为O(1)


请您通过
封锁队列
好吗。 这是我在互联网上找到的最好的链接-
BlockingQueue
是一个接口,它在包中-java.util.concurrent,它有多个实现:-

  • ArrayBlockingQueue
  • DelayQueue
  • LinkedBlockingQueue
  • PriorityBlockingQueue
  • SynchronousQueue

  • 请您通过
    封锁队列
    好吗。 这是我在互联网上找到的最好的链接-
    BlockingQueue
    是一个接口,它在包中-java.util.concurrent,它有多个实现:-

  • ArrayBlockingQueue
  • DelayQueue
  • LinkedBlockingQueue
  • PriorityBlockingQueue
  • SynchronousQueue

  • BlockingQueue

    [……]

    阻塞队列可能有容量限制

    ArrayBlockingQueue

    下面是您将如何使用它编写示例:

    BlockingQueue queue = new ArrayBlockingQueue<>(10000);
    while (true) {
        String tmp = new randomString();
        if (!queue.offer(tmp)) {
            // the limit was reached, item was not added
        }
    }
    
    BlockingQueue=newarrayblockingqueue(10000);
    while(true){
    字符串tmp=新的随机字符串();
    如果(!queue.offer(tmp)){
    //已达到限制,未添加项
    }
    }
    
    或者对于一个简单的生产者/消费者示例

    public static void main(String[] args) {
        // using a low limit so it doesn't take too long for the queue to fill
        BlockingQueue<String> queue = new ArrayBlockingQueue<>(10);
    
        Runnable producer = () -> {
            if (!queue.offer(randomString())) {
                System.out.println("queue was full!");
            }
        };
        Runnable consumer = () -> {
            try {
                queue.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(4);
        // produce faster than consume so the queue becomes full eventually
        executor.scheduleAtFixedRate(producer, 0, 100, TimeUnit.MILLISECONDS);
        executor.scheduleAtFixedRate(consumer, 0, 200, TimeUnit.MILLISECONDS);
    }
    
    publicstaticvoidmain(字符串[]args){
    //使用较低的限制,这样队列就不会占用太长时间
    BlockingQueue=new ArrayBlockingQueue(10);
    可运行生产者=()->{
    如果(!queue.offer(randomString())){
    System.out.println(“队列已满!”);
    }
    };
    可运行消费者=()->{
    试一试{
    queue.take();
    }捕捉(中断异常e){
    e、 printStackTrace();
    }
    };
    ScheduledExecutorService executor=Executors.newScheduledThreadPool(4);
    //生产速度快于消费速度,因此队列最终会满
    executor.scheduleAtFixedRate(生产者,0,100,时间单位毫秒);
    executor.scheduleAtFixedRate(消费者,0,200,时间单位毫秒);
    }
    
    阻塞队列

    [……]

    阻塞队列可能有容量限制

    ArrayBlockingQueue

    下面是您将如何使用它编写示例:

    BlockingQueue queue = new ArrayBlockingQueue<>(10000);
    while (true) {
        String tmp = new randomString();
        if (!queue.offer(tmp)) {
            // the limit was reached, item was not added
        }
    }
    
    BlockingQueue=newarrayblockingqueue(10000);
    while(true){
    字符串tmp=新的随机字符串();
    如果(!queue.offer(tmp)){
    //已达到限制,未添加项
    }
    }
    
    或者对于一个简单的生产者/消费者示例

    public static void main(String[] args) {
        // using a low limit so it doesn't take too long for the queue to fill
        BlockingQueue<String> queue = new ArrayBlockingQueue<>(10);
    
        Runnable producer = () -> {
            if (!queue.offer(randomString())) {
                System.out.println("queue was full!");
            }
        };
        Runnable consumer = () -> {
            try {
                queue.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(4);
        // produce faster than consume so the queue becomes full eventually
        executor.scheduleAtFixedRate(producer, 0, 100, TimeUnit.MILLISECONDS);
        executor.scheduleAtFixedRate(consumer, 0, 200, TimeUnit.MILLISECONDS);
    }
    
    publicstaticvoidmain(字符串[]args){
    //使用较低的限制,这样队列就不会占用太长时间
    BlockingQueue=new ArrayBlockingQueue(10);
    可运行生产者=()->{
    如果(!queue.offer(randomString())){
    System.out.println(“队列已满!”);
    }
    };
    可运行消费者=()->{
    试一试{
    queue.take();
    }捕捉(中断异常e){
    e、 printStackTrace();
    }
    };
    ScheduledExecutorService executor=Executors.newScheduledThreadPool(4);
    //生产速度快于消费速度,因此队列最终会满
    executor.scheduleAtFixedRate(生产者,0,100,时间单位毫秒);
    executor.scheduleAtFixedRate(消费者,0,200,时间单位毫秒);
    }
    
    @OP:你已经接受了答案,而且答案也是正确的,但是你仍然提高了赏金,所以我假设你更多地是在寻找这个概念,所以我将对这一部分进行说明

    现在,您的问题是您对尺寸操作的
    O(n)
    不满意,因此这意味着您的解决方案是:

    • 数据结构应该能够告诉您队列已满
    • 大小操作应返回恒定时间的结果
    大小操作将给出
    O(n)是不常见的
    但由于在
    LinkedTransferQueue
    的情况下存在异步行为,因此将遍历整个队列以确保队列中的元素数。否则,大多数队列实现会在固定时间内给出大小结果,但您确实不需要进行此大小检查,请继续阅读

    如果您对
    LinkedTransferQueue
    的目的有严格的依赖性,即您希望根据某个制作人的元素在队列中的时间长短来出列,那么我认为除了您可以做一些肮脏的事情,例如扩展
    LinkedTransferQueue
    ,然后跟踪您需要的元素数量之外,没有其他选择但很快它就会变得一团糟,无法给出准确的结果,也可能给出近似的结果

    如果您对
    LinkedTransferQueue
    没有任何硬依赖性,那么您可以使用某种类型的
    BlockingQueue
    ,其中许多使您能够以某种方式拥有“有界”队列(有界队列是您所需要的),例如,
    ArrayBlockingQueue