Hashmap concurrency issue

后端 未结 9 786
南旧
南旧 2020-11-30 01:54

I have a Hashmap that, for speed reasons, I would like to not require locking on. Will updating it and accessing it at the same time cause any issues, assuming I don\'t min

相关标签:
9条回答
  • 2020-11-30 02:39

    Yes. Very Bad Things will happen. For example, your thread might get stuck in an infinite loop.

    Either use ConcurrentHashMap, or NonBlockingHashMap

    0 讨论(0)
  • 2020-11-30 02:43

    Yes, it will cause major problems. One example is what could happen when adding a value to the hash map: this can cause a rehash of the table, and if that occurs while another thread is iterating over a collision list (a hash table "bucket"), that thread could erroneously fail to find a key that exists in the map. HashMap is explicitly unsafe for concurrent use.

    Use ConcurrentHashMap instead.

    0 讨论(0)
  • 2020-11-30 02:44

    I read here or elsewhere, no, you don't access from multi thread, but noone says what's really happen.

    So, I seen today (that's why I'm on this - old - question) on a application running in production since March : 2 put on the same HashSet (then HashMap) cause a CPU overload (near 100%), and memory increasing of 3GB, then down by GC. We have to restart the app.

    0 讨论(0)
提交回复
热议问题