Object creation (state initialisation) and thread safety

后端 未结 4 1034
你的背包
你的背包 2021-01-24 17:18

I am look into the book \"Java Concurrency in Practice\" and found really hard to believe below quoted statement (But unfortunately it make sense).

http://www.informit.c

4条回答
  •  被撕碎了的回忆
    2021-01-24 17:24

    the comment:

    the Object constructor first writes the default values to all fields before subclass constructors run

    seems wrong. My prior experience is that the default values for a class are set before its constructor is run. that is a super class will see its init-ed variables set before its constructor runs and does things. This was root of bug a friend looked at where a base class was calling a method during construction that the super class implemented and set a reference that was defined with initialization to null in the super class. the item would be there until entry to the constructor at which time the init set it to null value.

    references to the object are not available to another thread (assuming none generated in the constructor) until it completes construction and object reference is returned.

提交回复
热议问题