My question is about the synchronizedList Collections Class method.
Javadocs say:
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();
Although other methods do not require manual synchronization. I looked at the source code for the Collections class and found the shyncronization has already been reviewed for all methods like add
public boolean add(E e) { synchronized(list) {return c.add(e);} }
but not for the iterator method . I think the iterator method could also handle synchronization in the same way as above (this would avoid the extra work and manual synchronization for programmers). I'm sure there must be some specific reason for this, but am I missing it?
public Iterator<E> iterator() { return c.iterator();
A way to avoid manual synchronization with Programmer
public Iterator<E> iterator() { synchronized(list) { return c.iterator();
java collections list synchronization java.util.concurrent
M sach
source share