HashMap and visibility

前端 未结 3 947
终归单人心
终归单人心 2021-02-13 11:33

HashMap\'s javadoc states:

if the map is structurally modified at any time after the iterator is created, in any way except through the iterator\'s own re

3条回答
  •  不思量自难忘°
    2021-02-13 12:23

    As a side note, ConcurrentModificationException (despite the unfortunate name) is not intended to detect modification across multiple threads. it is only intended to catch modifications within a single thread. the effects of modifying a shared HashMap across multiple threads (without correct synchronization) are guaranteed to be broken regardless of the use of iterators or anything else.

    In short, your test is bogus regardless of the jvm version and it is only "luck" that it does anything different at all. for example, this test could throw NPE or some other "impossible" exception due to the HashMap internals being in an inconsistent state when viewed cross thread.

提交回复
热议问题