Monday 5 October 2015

Why cannot Java constructors be synchronized?

According to the Java Language Specification, constructors cannot be marked synchronized because other threads cannot see the object being created until the thread creating it has finished it.

Syncronized on the constructor would prevent two threads from calling the constructor on the same Object simultaneously, and that is not possible, as it is never possible to call the constructor on an object instance twice, period.

And when the constructor is called you still don't have the object. It is logically impossible for two threads to access the constructor of the same object.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...