Java concurrency scenario — do I need synchronization or not?

后端 未结 10 1778
陌清茗
陌清茗 2021-02-01 07:19

Here\'s the deal. I have a hash map containing data I call \"program codes\", it lives in an object, like so:

Class Metadata
{
    private HashMap validProgramC         


        
10条回答
  •  情书的邮戳
    2021-02-01 08:14

    I think your assumptions are correct. The only thing I would do is set the validProgramCodes volatile.

    private volatile HashMap validProgramCodes;
    

    This way, when you update the "pointer" of validProgramCodes you guaranty that all threads access the same latest HasMap "pointer" because they don't rely on local thread cache and go directly to memory.

提交回复
热议问题