Creating Object in a thread safety way

前端 未结 8 2240
再見小時候
再見小時候 2021-02-06 12:50

Directly from this web site, I came across the following description about creating object thread safety.

Warning: When constructing an object that will b

8条回答
  •  一向
    一向 (楼主)
    2021-02-06 13:33

    You just have to make sure, that even, when one thread hasn't initialized the Object, no Thread will access it (and get a NullpointerException).

    In this case, it would happen in the constructor (I suppose), but another thread could access that very object between its add to the list and the end of the constructor.

提交回复
热议问题