Why to synchronize on SynchronizedMap or SynchronizedCollections?

前端 未结 2 1406
耶瑟儿~
耶瑟儿~ 2021-02-02 16:24

I am referring to question asked here and using authors code example, now my question is

  1. Why does author uses synchronized(synchronizedMap), is it re
2条回答
  •  春和景丽
    2021-02-02 16:45

    It is not just about updating synchronizedMap values, it is about sequence of operations effecting the map. There are two operations happening on the map inside same method.

    If you don't synchronize block/method, assume there may be case like Thread1 executing first part and thread2 executing second part, your business operation may results in weird results (even though updates to map are synchronized)

提交回复
热议问题