为什么在使用LinkedBlockingQueue时使用ConcurrentLinkedQueue?

sdi*_*ver 9 java concurrency multithreading

ConcurrentLinkedQueue什么时候才能使用LinkedBlockingQueue?我知道ConcurrentLinkedQueue是非阻塞但LinkedBlockingQueue可以作为ConcurrentLinkedQueue.我会使用put()/ offer()方法进行插入和 poll()删除方法.poll()如果队列为空,则方法不等待.LinkedBlockingQueue也是无限的.所以我可以用它.

我迄今发现的区别ConcurrentLinkedQueue 是在使用硬件级同步机制比较和交换LinkedBlockingQueue使用ReentrantLock.

das*_*ght 5

主要的区别是,ConcurrentLinkedQueue无等待,不仅无锁(有什么区别?),同时LinkedBlockingQueue必须保持其本质锁定.

您可以ConcurrentLinkedQueue使用LinkedBlockingQueue和建模poll,但实现将保持锁定,从而减少您可能从系统中获得的并发性.

这是一个公认的不精确的微基准测试,它可以检查通过两个并发队列中的每个队列传递10,000个对象而不会阻塞的速度,并计算poll()循环中调用的总数:

AtomicInteger total = new AtomicInteger();
ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<>();
Thread thread = new Thread(() -> {
    int remaining = 10000;
    while (remaining != 0) {
        total.incrementAndGet();
        if (q.poll() != null) {
            remaining--;
        }
    }
});
Integer[] first100 = new Integer[100];
for (int i = 0 ; i != 100 ; i++) {
    first100[i] = i;
}
long start = System.nanoTime();
thread.start();
for (int i = 0 ; i != 10000 ; i++) {
    q.add(first100[i%100]);
}
thread.join();
long runtime = System.nanoTime() - start;
Run Code Online (Sandbox Code Playgroud)

这个想法是在没有其他处理的情况下测量队列的"吞吐量".此任务在11.23 ms内完成,在读取线程(演示1)中进行60K次迭代ConcurrentLinkedQueue,并使用LinkedBlockingQueue(演示2)进行23,46 ms/100K迭代.