WeakHashMap iteration and garbage collection

前端 未结 4 1588
难免孤独
难免孤独 2020-12-10 03:30

I am using a WeaekHashMap to implement a Cache. I am wondering if I am iterating over the keys of this map, and at the same time garbage collector is actively r

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-10 03:46

    No, you will not receive ConcurrentModificationException. WeakHashMap uses ReferenceQueue.poll when you call various operations. In other words, each caller is silently responsible for clearing stale entries from the Map. However, that does mean that it is not safe to call methods on a WeakHashMap from multiple threads that would otherwise seem to be "read-only" because any call to get() can destroy the entry linked list that another thread is attempting to iterate.

提交回复
热议问题