本文介绍了ConcurrentLinkedDeque与LinkedBlockingDeque的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

即如果不考虑阻塞方面,除了LinkedBlockingDeque受限制之外,ConcurrentLinkedDequeLinkedBlockingDeque之间还有其他区别吗?

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

推荐答案

两件事:

1::如果我仅使用LinkedBlockingDeque中的非阻塞方法,例如addFirst()removeFirst(),它与ConcurrentLinkedDeque有什么区别吗?

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

LinkedBlockingDeque中,这些方法在并发锁定行为方面确实有所不同:

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

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

addFirst方法类似.在ConcurrentLinkedDeque中,这两种方法的锁定行为都不同,并且效率更高,因为它不会锁定整个列表,而是锁定它的一部分,检查ConcurrentLinkedDeque的源将使您对此更加清楚.

Similarly for addFirst method. In ConcurrentLinkedDeque this 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 ConcurrentLinkedDeque will give you more clarity on this.

2 :来自ConcurrentLinkedDeque的javadoc:

2: From javadoc of ConcurrentLinkedDeque:

..

此外,批量操作addAll,removeAll,retainAll, 不保证可以执行containsAll,equals和toArray 原子地.

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

以上对于LinkedBlockingDeque

这篇关于ConcurrentLinkedDeque与LinkedBlockingDeque的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 15:18