R
Richard Maher
Hi,
WRT JavaDocs for the ArrayList class: -
Note that this implementation is not synchronized. If multiple threads
access an ArrayList instance concurrently, and at least one of the threads
modifies the list structurally, it must be synchronized externally. (A
structural modification is any operation that adds or deletes one or more
elements, or explicitly resizes the backing array; merely setting the value
of an element is not a structural modification.) This is typically
accomplished by synchronizing on some object that naturally encapsulates the
list. If no such object exists, the list should be "wrapped" using the
Collections.synchronizedList method. This is best done at creation time, to
prevent accidental unsynchronized access to the list:
List list = Collections.synchronizedList(new ArrayList(...));
and so on. . .
Can someone please explain why locking/synchronizing on the ArrayList
instance itself is not sufficent to serialize access?
eg: -
ArrayList<MyObj> myList = new ArrayList<MyObj>();
synchronized (myList) {
myList.add(aMyObj1);
lotsoffatomicisolatedstuff();
}
Are wrapper classes like collections really necessary here?
Cheers Richard Maher
WRT JavaDocs for the ArrayList class: -
Note that this implementation is not synchronized. If multiple threads
access an ArrayList instance concurrently, and at least one of the threads
modifies the list structurally, it must be synchronized externally. (A
structural modification is any operation that adds or deletes one or more
elements, or explicitly resizes the backing array; merely setting the value
of an element is not a structural modification.) This is typically
accomplished by synchronizing on some object that naturally encapsulates the
list. If no such object exists, the list should be "wrapped" using the
Collections.synchronizedList method. This is best done at creation time, to
prevent accidental unsynchronized access to the list:
List list = Collections.synchronizedList(new ArrayList(...));
and so on. . .
Can someone please explain why locking/synchronizing on the ArrayList
instance itself is not sufficent to serialize access?
eg: -
ArrayList<MyObj> myList = new ArrayList<MyObj>();
synchronized (myList) {
myList.add(aMyObj1);
lotsoffatomicisolatedstuff();
}
Are wrapper classes like collections really necessary here?
Cheers Richard Maher