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

前端 未结 2 346
傲寒
傲寒 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<Integer> myList;
    
      //Constructor
      public SynchroProblem(List<Integer> 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<Integer> iterator = myList.iterator();
          while (iterator.hasNext()){
            int number = iterator.next();
            if (number == 123){
              iterator.remove();
            }
          }
    
        }
      }
    
      public static void main(String[] args) {
    
        List<Integer> originalList = new ArrayList<Integer>();
    
        // Synchronize list
        List<Integer> 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();
    
      }
    }
    
    0 讨论(0)
  • 2020-12-03 03:42

    The list being synchronized only means that add, remove etc. operations are synchronized and therefore atomic. Iteration however is not and if a thread adds while another is iterating, you could get a ConcurrentModificationException.

    By manually synchronizing your iteration block, you ensure that the list is not modified while iterating.

    One alternative is to use a CopyOnWriteArrayList which provides an iterator that iterates over the list as it was known when the iteration started, regardless of subsequent modifications. That collection is however not very efficient if you need to change the content of the list very often.

    0 讨论(0)
提交回复
热议问题