Is java.util.Hashtable thread safe?

后端 未结 9 1264
名媛妹妹
名媛妹妹 2020-12-09 06:23

It\'s been a while since I\'ve used hashtable for anything significant, but I seem to recall the get() and put() methods being synchronized.

The JavaDocs don\'t ref

9条回答
  •  萌比男神i
    2020-12-09 07:09

    For general usage it is thread safe.

    But you have to understand that it doesent make your application logic around it thread-safe. For e.g. consider implementing to put a value in a map, if its not there already. This idiom is called putIfAbsent. Its difficult to implement this in a thread-safe manner using HashTable alone. Similarly for the idiom replace(k,V,V).

    Hence for certain idioms like putIfAbsent and and replace(K,V,V), I would recommend using ConcurrentHashMap

提交回复
热议问题