关于多线程:构造函数中的过早泄漏

Premature leak in constructor

Java docs关于构造函数的同步陈述如下:

Note that constructors cannot be synchronized a€" using the synchronized keyword with a constructor is a syntax error. Synchronizing constructors doesn't make sense, because only the thread that creates an object should have access to it while it is being constructed.

Warning: When constructing an object that will be shared between
threads, be very careful that a reference to the object does not
"leak" prematurely. For example, suppose you want to maintain a List
called instances containing every instance of class. You might be
tempted to add the following line to your constructor:
instances.add(this); But then other threads can use instances to
access the object before construction of the object is complete.

我无法理解这整个过程。首先,它声明只有创建对象的线程才能访问构造函数。然后,它警告过早泄漏,如果其他线程在构造完成之前访问该对象,则可能导致问题。这两件事不是矛盾的。如果只有创建线程可以访问构造函数,那么其他线程如何过早访问该对象,因为只有在构造函数完全运行后才可以访问该对象?
任何输入都会有很大帮助。


想象一下两个线程都可以访问包含相关类实例的全局列表(称为"实例")。线程1不断循环遍历列表,并对每个实例执行操作。线程2拥有自己独特的方式,并偶尔构造该类的新实例。如果该类将自身添加到其构造函数中的List中(使用instances.add(this)),线程1将立即访问该实例,并且可以在实例完全构建之前对其进行操作,从而导致不可预测的行为。

" should"一词可能会有误解。您写道:"首先,它声明只有创建对象的线程才可以访问构造函数。"但是,Java文档说:"只有创建对象的线程才可以在构造对象时对其进行访问",这意味着注意,在构造对象时,只有一个线程可以访问该对象。