I've seen the use cases of SynchronizedList - they state that upon iterating, even though SynchronizedList is thread-safe, we should use an iterator and a synchronized block like so -
synchronized(myList){
Iterator<Item> iterator = myList.iterator();
while (iterator.hasNext())
{
System.out.println(iterator.next().getMessage());
}
}
If I use a ConcurrentHashSet for example (possible in Java 8 using newKeySet() of concurrentHashMap), in a multi-threaded environment, is it still necessary to extract an iterator and use a synchronized block? I tried testing it and it seems unnecessary but I might be missing something.
Thank you!