synchronized collections

 

Collections provides six make-thread-safe methods:

 

synchronizedCollection(Coll)

synchronizedList(List)

synchronizedCMap(Map)

synchronizedSortedMap(SortedMap)

synchronizedeSet(Set)

synchronizedSortedSet(SortedSet)

 

■ HashSet is not synchronized.  You must synchronize on it yourself if multiple threads will be concurrently accessing and updating it. 

Wrap it at creation time using the synchronizedSet(...) method. i.e.

     Set hs = Collections.synchronizedSet(new HashSet(...));