(Java) Why is a HashSet allowed to use synchronously if it is not synchronized?

I read about the differences between HashMap, HashSetand HashTable. The main thing that I noticed is that I saw that HashMap/ are HashSetnot in sync as well HashTable.

However, in the code base that I saw earlier, there are several places where such a block is used:

synchronized (hashSet) {
    //Some code involving the hashset 
}

How is this possible if HashSetnot synchronized? A synchronized block simply allows us to use a non-synchronous data structure, as if it were synchronized?

If they HashSetwere synchronized, we just would not need to include a synchronized () {} block?

+4
source share
1 answer

A synchronized . , HashSet , , , , (, synchronizing HashSet, ).

+5

Source: https://habr.com/ru/post/1690043/


All Articles