aspose file tools
The moose likes Threads and Synchronization and the fly likes Is thread-safety a concern for constructors? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Is thread-safety a concern for constructors?" Watch "Is thread-safety a concern for constructors?" New topic
Author

Is thread-safety a concern for constructors?

boyet silverio
Ranch Hand

Joined: Aug 28, 2002
Posts: 173
Just in case. What are the situations?
Thanks in advance.
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Two off the top of my head:
1) If the contructor should be called only once
2) If the constructor code conditionally modifies a static member (known as a test-and-set routine).


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
Note that some code somewhere has to call something like:
new TheObject()
for the constructor to be run.
Since a constructor is only run while the object is being created, thread safety of the constructor itself is not an issue. This is because two threads can't create the same object. If they are creating objects, they will create different ones.
Thus it does not make sense to have a synchronized keyword for a constructor.
Now what the constructor is doing is another matter. If it is accessing some common resource (another object or static methods/fields) then there could be a problem. But this is no different than any method's access of those resources. Those resources should be protected in either case.
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Originally posted by Dave Landers:
Note that some code somewhere has to call something like:
new TheObject()
for the constructor to be run.

This is, in the strictest technical sense, not the case. Consider:
1) constructors call other constructors in the same class if they are chained together (as in this());
2) child constructors call parent constructors prior to completing.
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
OK, to clarify what I meant.
In order for an object to be constructed (and thus have some combination of constructors invoked) somebody has to make a call to new.
While that is going on, other threads can not get a reference to that object until it is constructed (exception -see below). And in any case when another gets the instance, it can not invoke the constructor. A second threads can not invoke any constructor on the same object instance.
Therefore the constructors themselves are "thread-safe", and the only concern is the thread safety of objects that are used by those constructors.
Exception: What I said above about "other threads can not get a reference till it is constructed" is not strictly true. It is true enough for the explaination above, but it still has a hole. The reason is all wrapped up in the Java Memory Model. Its the same reason that Double Checked Locking doesn't work in Java. Don't need to go into that into too much detail here.
boyet silverio
Ranch Hand

Joined: Aug 28, 2002
Posts: 173
Thanks for your inputs guys. They got certain constructor issues cleared.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Slightly relevant:
Some things not to do within a constructor:
a)let the reference "this" to scape. For instance recording it in a field, or calling a method passing it as argument.
b)calling methods that could be overriden
They both could try to access the instance fields before they are fully initialized.


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Is thread-safety a concern for constructors?