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
Two things:
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
?
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();
}
}
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: From javadoc of 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.
Above is not true for LinkedBlockingDeque
First thing both LinkedBlockingDeque and ConcurrentLinkedDeque both are thread safe but which one to use depends on your application requirement.
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.
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.
As in your question,
1. I need to have a thread-safe LIFO structure,
Use LinkedBlockingDeque if at a time you want only single thread can operate your data.
Use ConcurrentLinkedDeque if you want that each thread can access the shared data
2. If you disregard the blocking aspect, is there any other difference between ConcurrentLinkedDeque and 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.
To quote the great Doug Lea (my emphasis)
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.
He seems to be suggesting that you should use LinkedBlockingDeque
unless you specifically need the features of ConcurrentLinkedDeque
.
ConcurentLinkedDequeue is lock-free (see comments in source code) while LinkedBlockingQueue uses locking. That is the former is supposed to be more efficient