You can use a ConcurrentLinkedQueue
for this since both offer(E)
and poll()
process your most likely operations in constant time. (I suggested a ConcurrentLinkedDeque
first, but I misread that you wanted to add and remove elements at the same side of the queue. Since this is not a requirement prefer the one-sided queue.)
The only downside of this choice is that you would need to iterate over n
entries of the Deque
in order to remove the n
-th element. Also, this operation would be blocking or would offer fail-potential since the javadoc states:
The returned iterator is a "weakly consistent" iterator that will
never throw ConcurrentModificationException, and guarantees to
traverse elements as they existed upon construction of the iterator,
and may (but is not guaranteed to) reflect any modifications
subsequent to construction.
Other than that, this queue will perform very well on the two other tasks (adding or removing from both ends) since the queue is non-blocking for concurrent operations what is something you want when efficiency matters.