Why do I need to synchronize a list returned by Collections.synchronizedList

前端 未结 2 360
傲寒
傲寒 2020-12-03 02:52

i found this at dos.oracle.com

public static List synchronizedList(List list)

Returns a synchronized (thread-safe) list backed by the spe

2条回答
  •  失恋的感觉
    2020-12-03 03:40

    I was a bit confused by this topic at first because most example I found were without a context. I finally found this blog post that cleared things up for me: http://netjs.blogspot.de/2015/09/how-and-why-to-synchronize-arraylist-in-java.html

    From the example above it looks like I just have to convert my list with Collections.synchronizeList() and then I can add and remove items without worrying about thread safety. But here it is important to note that you have to synchronize the list before it is passed to the different threads because otherwise the lists access is not mutually exclusive.

    So a complete example would be:

    public class SynchroProblem implements Runnable{
      private List myList;
    
      //Constructor
      public SynchroProblem(List myList){
        this.myList = myList;
      }
    
      @Override
      public void run() {
        // Do stuff with the list .add(), .remove(), ...
        myList.add(5);
    
        // Even if mylist is synchronized the iterator is not, 
        // so for using the iterator we need the synchronized block
        synchronized (myList){
          // do stuff with iterator e.g.
          Iterator iterator = myList.iterator();
          while (iterator.hasNext()){
            int number = iterator.next();
            if (number == 123){
              iterator.remove();
            }
          }
    
        }
      }
    
      public static void main(String[] args) {
    
        List originalList = new ArrayList();
    
        // Synchronize list
        List syncList = Collections.synchronizedList(originalList);
    
        // Create threads and pass the synchronized list
        Thread t1 = new Thread(new SynchroProblem(syncList));
        Thread t2 = new Thread(new SynchroProblem(syncList));
    
        t1.start();
        t2.start();
    
      }
    }
    

提交回复
热议问题