Java ConcurrentLinkedDeque 与 LinkedBlockingDeque

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19179046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:47:54  来源:igfitidea点击:

ConcurrentLinkedDeque vs LinkedBlockingDeque

javamultithreadingdata-structuresconcurrency

提问by Nufail

I need to have a thread-safe LIFO structure and found that I can use thread-safe implementations of Dequefor this. Java 7 has introduced ConcurrentLinkedDequeand Java 6 has LinkedBlockingDeque.

我需要一个线程安全的 LIFO 结构,并发现我可以Deque为此使用线程安全的实现。Java 7 引入了ConcurrentLinkedDeque,Java 6引入了LinkedBlockingDeque.

If I were to use only the non-blocking methods in LinkedBlockingDequesuch as addFirst()and removeFirst()does it have any difference to ConcurrentLinkedDeque?

如果我是只使用非阻塞的方法LinkedBlockingDeque,例如addFirst()removeFirst()它有什么区别ConcurrentLinkedDeque

i.e. If you disregard the blocking aspect, is there any other difference between ConcurrentLinkedDequeand LinkedBlockingDeque, apart from LinkedBlockingDequebeing bounded?

即如果您不考虑阻塞方面,除了有界之外,ConcurrentLinkedDeque和之间还有其他区别吗?LinkedBlockingDequeLinkedBlockingDeque

采纳答案by harsh

Two things:

两件事情:

1:If I were to use only the non-blocking methods in LinkedBlockingDequesuch as addFirst()and removeFirst()does it have any difference to ConcurrentLinkedDeque?

1:如果我是只使用非阻塞的方法LinkedBlockingDeque,例如addFirst()removeFirst()它有什么区别ConcurrentLinkedDeque

These methods do have difference in terms of concurrent locking behavior, in LinkedBlockingDeque:

这些方法在并发锁定行为方面确实存在差异,在LinkedBlockingDeque

public E removeFirst() {
        E x = pollFirst();
        ..
    }
 public E pollFirst() {
        lock.lock(); //Common lock for while list
        try {
            return unlinkFirst();
        } finally {
            lock.unlock();
        }
    }

Similarly for addFirstmethod. In ConcurrentLinkedDequethis locking behavior for both the method is different and is more efficient as it doesn't lock the whole list but a subset of it, checking source for ConcurrentLinkedDequewill give you more clarity on this.

addFirst方法类似。在ConcurrentLinkedDeque这种锁定行为中,这两种方法是不同的并且更有效,因为它不会锁定整个列表而是它的一个子集,检查源代码ConcurrentLinkedDeque将使您更清楚地了解这一点。

2:From javadoc of ConcurrentLinkedDeque:

2:来自的javadoc ConcurrentLinkedDeque

Beware that, unlike in most collections, the size method is NOT a constant-time operation.

..

Additionally, the bulk operations addAll, removeAll, retainAll, containsAll, equals, and toArray are not guaranteed to be performed atomically.

请注意,与大多数集合不同,size 方法不是恒定时间操作。

..

此外,批量操作 addAll、removeAll、retainAll、containsAll、equals 和 toArray 不能保证以原子方式执行。

Above is not true for LinkedBlockingDeque

以上不适用于 LinkedBlockingDeque

回答by Evgeniy Dorofeev

ConcurentLinkedDequeue is lock-free (see comments in source code) while LinkedBlockingQueue uses locking. That is the former is supposed to be more efficient

ConcurentLinkedDequeue 是无锁的(参见源代码中的注释),而 LinkedBlockingQueue 使用锁。那是前者应该更有效率

回答by OldCurmudgeon

To quote the great Doug Lea(my emphasis)

引用伟大的Doug Lea(我的重点)

LinkedBlockingDeque vs ConcurrentLinkedDeque

The LinkedBlockingDeque class is intended to be the "standard" blocking deque class.The current implementation has relatively low overhead but relatively poor scalability. ...

... ConcurrentLinkedDeque has almost the opposite performance profile as LinkedBlockingDeque: relatively high overhead, but very good scalability. ... in concurrent applications, it is not all that common to want a Deque that is thread safe yet does not support blocking. And most of those that do are probably better off with special-case solutions.

LinkedBlockingDeque 与 ConcurrentLinkedDeque

LinkedBlockingDeque 类旨在成为“标准”阻塞双端队列类。当前实现的开销相对较低,但可扩展性相对较差。...

... ConcurrentLinkedDeque 具有与 LinkedBlockingDeque 几乎相反的性能配置文件相对较高的开销,但非常好的可扩展性。...在并发应用程序中,想要一个线程安全但不支持阻塞的 Deque 并不常见。大多数这样做的人可能会更好地使用特殊情况的解决方案。

He seems to be suggesting that you should use LinkedBlockingDequeunless you specifically need the features of ConcurrentLinkedDeque.

他似乎建议你应该使用,LinkedBlockingDeque除非你特别需要ConcurrentLinkedDeque.

回答by CrazyCoder

First thing both LinkedBlockingDeque and ConcurrentLinkedDeque both are thread safe but which one to use depends on your application requirement.

首先,LinkedBlockingDeque 和 ConcurrentLinkedDeque 都是线程安全的,但使用哪个取决于您的应用程序要求。

For example,

例如,

LinkedBlockingDequeue :Use this collection if you want that at a time only single thread can operate on your data and when you need blocking for your application.

LinkedBlockingDequeue :如果您希望一次只有单个线程可以对您的数据进行操作,并且当您需要阻止应用程序时,请使用此集合。

ConcurrentLinkedDeque:This is also thread safe collection deque, If you application is multi threaded and you want that each one of your thread can access the data then ConcurrentLinkedDequeue is the best choise for it.

ConcurrentLinkedDeque:这也是线程安全的集合双端队列,如果您的应用程序是多线程的并且您希望您的每个线程都可以访问数据,那么 ConcurrentLinkedDequeue 是它的最佳选择。

As in your question,

正如你的问题,

1. I need to have a thread-safe LIFO structure,

1. 我需要一个线程安全的 LIFO 结构,

Use LinkedBlockingDeque if at a time you want only single thread can operate your data.

如果您一次只希望单个线程可以操作您的数据,请使用 LinkedBlockingDeque。

Use ConcurrentLinkedDeque if you want that each thread can access the shared data

如果您希望每个线程都可以访问共享数据,请使用 ConcurrentLinkedDeque

2. If you disregard the blocking aspect, is there any other difference between ConcurrentLinkedDeque and LinkedBlockingDeque,

2.如果不考虑阻塞方面,ConcurrentLinkedDeque和LinkedBlockingDeque还有其他区别吗,

Yes, there is a difference as LinkedBlockingDeque is using locking mechanism and ConcurrentLinkedDeque is not this may affect the performance when you want to operate your data.

是的,这是有区别的,因为 LinkedBlockingDeque 使用锁定机制,而 ConcurrentLinkedDeque 不是这可能会影响您想要操作数据时的性能。