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

2

25

i found this at dos.oracle.com

public static List synchronizedList(List list)

Returns a synchronized (thread-safe) list backed by the specified list. In order to guarantee serial access, it is critical that all access to the backing list is accomplished through the returned list. It is imperative that the user manually synchronize on the returned list when iterating over it:

  List list = Collections.synchronizedList(new ArrayList());
      ...
  synchronized(list) {
      Iterator i = list.iterator(); // Must be in synchronized block
      while (i.hasNext())
          foo(i.next());
  }

My question is : Why do i have to Synchronize the list to iterate it if Collections.synchronizedList(); is supposed to return an already synchronized list ?

Im just accesing the list in two threads: One Thread just add and the other thread to get and delete. What other classes you recommend to use for this scenario ?

Thanks for reading.

Tailstock answered 1/8, 2013 at 12:42 Comment(0)
T
33

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.

Towboat answered 1/8, 2013 at 12:45 Comment(5)
Thanks! I would upvote it but i don't have enough reputation. CopyOnWriteArrayList sounds good, but yes, is a very very often process, CopyOnWriteArrayList might be kinda slow.Tailstock
+1. How do you know add, remote, etc are synchronized? There is nothing about it in the javadoc.David
@LuisSep you can conclude that based on the fact that the javadoc says that the list is synchronized and iteration should acquire the list's monitor (which is consistent with the methods being synchronized). Having a look at the implementation confirms it.Towboat
So if I have to add the synchronized(list){...} block, do I even need a synchronized list then or could I just use the normal ArrayList inside of this block?Riffe
@Riffe you only need to use the synchronized keyword to iterate - the "simple" operations such as add, remove etc. are atomic. If you use an ArrayList you would need to synchronize those calls too.Towboat
R
-1

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();

  }
}
Riffe answered 23/9, 2016 at 13:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.