Is LinkedList thread-safe when I'm accessing it with offer and poll exclusively? Is LinkedList thread-safe when I'm accessing it with offer and poll exclusively? multithreading multithreading

Is LinkedList thread-safe when I'm accessing it with offer and poll exclusively?


LinkedList is not thread safe. You'd have to do the locking yourself.

Try ConcurrentLinkedQueue or LinkedBlockingDeque instead if it fits your needs, they are thread safe but slightly different behavior than LinkedList.


if you have a JDK, you can look at the source code of "Collections.synchronizedList()". It is simple, so you can create a copy of this method specialized to get both LinkedList and synchronization functionnalities.

public class SynchronizedLinkedList<T> implements List<T> {    private LinkedList<T> list;    private Object lock;    public void add(T object) {        synchronized(lock) {            list.add(object);        }    }    // etc.}