11. The collection class is not safe (set unsafe solution (CopyOnWriteArraySet)) and the underlying analysis of HashSet

The collection class is unsafe (set unsafe solution (CopyOnWriteArraySet)) and the underlying analysis of HashSet

relation

 

Problem with 30 threads

 

Or ConcurrModficationException. concurrent modification exception

 

Solutions:

Solution 1: Still a tool class Collections.synchronizedSet (new HashSet<>)

 

 

Scheme 2, the same copy-on-write under JUC (only here becomes CopyOnWriteArraySet<>())

 

! ! ! What is the bottom layer of HashSet:

  1. The bottom layer of HashSet is a HashMap, and why this HashSet uses the key of HashMap (this ensures that it is not repeated (the key is not repeated))

 

It uses map.put a key (the key is not repeated)

 

Guess you like

Origin blog.csdn.net/logtcm4/article/details/127859716