Creating Object in a thread safety way

前端 未结 8 2254
再見小時候
再見小時候 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:14

    Its describing the following situation:

    Thread1:
     //we add a reference to this thread
     object.add(thread1Id,this);
     //we start to initialize this thread, but suppose before reaching the next line we switch threads
     this.initialize(); 
    Thread2:     
    //we are able to get th1, but its not initialized properly so its in an invalid state 
    //and hence th1 is not valid
    Object th1 = object.get(thread1Id); 
    

提交回复
热议问题