在java 7/8中有大小限制的链接传输队列的替代方案?

By simon at 2018-02-28 • 0人收藏 • 53人看过

为了实施生产者/消费者patthern,我已经使用了 [linkedTransferQueue](https://docs.oracle.com/javase/7/docs/api/java/util/c并发/ LinkedTransferQueue.html)。 检查下面的代码

while (true) {

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

}
从文档中说它的大小是O(n)操作:(所以用于添加元素 它应该通过整个收集。 是否有任何其他并发收集队列具有大小restriction? 在[java标准中找不到任何东西 集合](http://www.logicbig.com/tutorials/core-java-tutorial/java- 集合/ concurrent-collection-cheatsheet /),[apache并发 集合](https://commons.apache.org/proper/commons- 收藏/ APIDOCS /组织/阿帕奇/公/ collections4 /列表/包summary.html) ?

4 个回复 | 最后更新于 2018-02-28
2018-02-28   #1

BlockingQueue是

[BlockingQueue实现是thread- 安全(https://docs.oracle.com/javase/8/docs/api/java/util/concurren吨/ BlockingQueue.html)

[...]

BlockingQueue可能受限于容量。 和ArrayBlockingQueue是 [有界阻塞队列由...支持 阵列(https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ArrayBlockingQueue.html) 以下是你的方式写下你的例子:

BlockingQueue queue = new ArrayBlockingQueue<>(10000);
while (true) {
    String tmp = new randomString();
    if (!queue.offer(tmp)) {
        // the limit was reached, item was not added
    }
}
或者为一个简单的生产者/消费者例子
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);
}

2018-02-28   #2

你有没有试过ArrayBlockingQueue? https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ArrayBlockingQueue.html 它有siz限制和并发。 另外,大小是O(1)。

public int size() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        return count;
    } finally {
        lock.unlock();
    }
}

2018-02-28   #3

你能否请通过BlockingQueue.这里是我发现的最好的链接 互联网 - [BlockingQueue](http://tutorials.jenkov的.com / java的util- 并发/ blockingqueue.html)。 BlockingQueue是一个界面,它在 包 - * _ java.util.concurrent _ *它有多个实现: -   1. ArrayBlockingQueue   2. DelayQueue   3. LinkedBlockingQueue   4. PriorityBlockingQueue   5. SynchronousQueue

2018-02-28   #4

BlockingQueue是

[BlockingQueue实现是thread- 安全(https://docs.oracle.com/javase/8/docs/api/java/util/concurren吨/ BlockingQueue.html)

[...]

BlockingQueue可能受限于容量。 和ArrayBlockingQueue是 [有界阻塞队列由...支持 阵列(https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ArrayBlockingQueue.html) 以下是你的方式写下你的例子:

BlockingQueue queue = new ArrayBlockingQueue<>(10000);
while (true) {
    String tmp = new randomString();
    if (!queue.offer(tmp)) {
        // the limit was reached, item was not added
    }
}
或者为一个简单的生产者/消费者例子
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);
}

登录后方可回帖

Loading...