Java Hashmap - Multiple thread put

前端 未结 2 2017
北海茫月
北海茫月 2021-01-12 21:39

We\'ve recently had a discussion at my work about whether we need to use ConcurrentHashMap or if we can simply use regular HashMap, in our multithreaded environment. The arg

2条回答
  •  再見小時候
    2021-01-12 22:04

    In multi thread environment, you should always use CuncurrentHashMap, if you are going to perform any operation except get.

    Most of the time you won't get an exception, but definitely get the corrupt data because of the thread local copy value.

    Every thread has its own copy of the Map data when performing the put operation and when they check for key existence, multiple threads found it false and they enter the data.

提交回复
热议问题