What is an “incompletely constructed object”?

前端 未结 3 1398
太阳男子
太阳男子 2020-11-28 12:14

Goetz\'s Java Concurrency in Practice, page 41, mentions how this reference can escape during construction. A \"don\'t do this\" example:

public         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 12:39

    There is a small but finite time between the registerListener ending and the constructor returning. Another thread could use come in at that time and attempt to call doSomething(). If the runtime didn't return straight to your code at that time, the object could be in a invalid state.

    I'm not sure of java really but one example I can think of is where possibly the runtime relocates the instance before returning to you.

    Its a small chance I grant you.

提交回复
热议问题