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

后端 未结 4 1828
长情又很酷
长情又很酷 2020-12-09 03:07

I have a linked list samples:

protected LinkedList samples = new LinkedList();

I\'m append

4条回答
  •  不思量自难忘°
    2020-12-09 03:28

    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 implements List {
    
        private LinkedList list;
    
        private Object lock;
    
        public void add(T object) {
            synchronized(lock) {
                list.add(object);
            }
        }
    
        // etc.
    }
    

提交回复
热议问题