I use Collections.synchronizedCollection in Java to protect a collection that I know is accessed simultaneously by multiple threads. Java API warns:
"It is imperative that the user manually synchronizes the returned collection when iterating over it:
Collection c = Collections.synchronizedCollection(myCollection);
...
synchronized(c) {
Iterator i = c.iterator();
while (i.hasNext())
foo(i.next());
}
"
If I use c.contains(obj), is it thread safe? Inside, obviously, iterating over the collection and observing if any of the objects in it is equal to obj. My instinct is to assume that it is probably synchronized (it would seem to be a serious failure, if not), but, given the previous problems with synchronization, it seems reasonable to double-check, and the Google search for answers to this did not turn out to be- sometime.