What is the difference in behavior between these t

2019-04-28 09:29发布

List<String> list = new ArrayList<String>();
list.add("a");
...
list.add("z");

synchronized(list) {
    Iterator<String> i = list.iterator();
    while(i.hasNext()) {
        ...
    }
}

and

List<String> list = new ArrayList<String>();
list.add("a");
...
list.add("z");

List<String> synchronizedList = Collections.synchronizedList(list);

synchronized(synchronizedList) {
    Iterator<String> i = synchronizedList.iterator();
    while(i.hasNext()) {
        ...
    }
}

Specifically, I'm not clear as to why synchronized is required in the second instance when a synchronized list provides thread-safe access to the list.

3条回答
Luminary・发光体
2楼-- · 2019-04-28 09:59

The second code needs to be synchronized because of the way synchronized lists are implemented. This is explained in the javadoc:

It is imperative that the user manually synchronize on the returned list when iterating over it

The main difference between the two code snippets is the effect of the add operations:

  • with the synchronized list, you have a visibility guarantee: other threads will see the newly added items if they call synchronizedList.get(..) for example.
  • with the ArrayList, other threads might not see the newly added items immediately - they might actually not ever see them.
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-04-28 10:03

If you don't lock around the iteration, you will get a ConcurrentModificationException if another thread modifies it during the loop.

Synchronizing all of the methods doesn't prevent that in the slightest.

This (and many other things) is why Collections.synchronized* is completely useless.
You should use the classes in java.util.concurrent. (and you should think carefully about how you will guarantee you will be safe)

As a general rule of thumb:

Slapping locks around every method is not enough to make something thread-safe.

For much more information, see my blog

查看更多
Viruses.
4楼-- · 2019-04-28 10:04

synchronizedList only makes each call atomic. In your case, the loop make multiple calls so between each call/iteration another thread can modify the list. If you use one of the concurrent collections, you don't have this problem.

To see how this collection differs from ArrayList.

List<String> list = new CopyOnWriteArrayList<String>();
list.addAll(Arrays.asList("a,b,c,d,e,f,g,h,z".split(",")));

for(String s: list) {
    System.out.print(s+" ");
    // would trigger a ConcurrentModifcationException with ArrayList
    list.clear(); 
}

Even though the list is cleared repeatedly, it prints the following because that wa the contents when the iterator was created.

a b c d e f g h z 
查看更多
登录 后发表回答