Is setting a HashMap thread safe?

后端 未结 4 2083
北荒
北荒 2021-02-20 10:39

I have a HashMap in my program which is accessed by multiple threads, and is occasionally set by a single thread.

For example:

Map

        
4条回答
  •  野的像风
    2021-02-20 11:01

    HashMap is not thread safe. You can use any of the followings

    1. ConcurrentHashMap.
    2. HashMap with synchronized on the outside.
    3. Different HashMap for each thread.

    Check this similar answer here

提交回复
热议问题