Hashmap and hashtable in a multi-threaded environment

I'm really confused about how these 2 collections behave in a multi-threaded environment.

The hash table is synchronized, which means that 2 threads will not update its value at the same time?

+5
source share
5 answers

Check out the ConcurrentHashMap for Secure Thread Cards.

They offer all HashTable features with high performance very close to HashMap.

Performance is achieved through the use of locks with a wide range of cards; the collection contains a list of 16 default locks, each of which is used to lock one bucket of cards. You can even adjust the number of buckets. Fine tuning can help in performance depending on your data.

I cannot recommend enough Java Concurrency to practice Brian Goetz http://jcip.net/

I still learn something new every time I read it.

+12
source

, Hashtable Collections.synchronizedMap . check-then-act, , , .

, :

  • : swapValues(Map, Object k1, Object k2)

  • : appendToValue(Map, Object k1, String suffix)

, JCIP: -)

+3

, HashTable , , ( HashTable). - , , .

HashMap , , .

+2

, , values ​​() (. docs).

, java.util.concurrent, .

0

Hashtables , , , . , (, ), .

, , , .

HashMaps, :

Map m = Collections.synchronizedMap(new HashMap());

which wraps the map in synchronous method calls. But it has the same concurrency disadvantages as Hashtable.

According to Paul, ConcurrentHashMaps provides thread-safe maps with additional useful methods for atomic updates.

0
source

All Articles