When you have a Constructor object in Java.
Is calling the newInstance method on the same Constructor object from multiple threads safe.
Or is this not thread safe leading to wrongly constructed objects?
When you have a Constructor object in Java.
Is calling the newInstance method on the same Constructor object from multiple threads safe.
Or is this not thread safe leading to wrongly constructed objects?
Though it is not explicitly stated (at least I didn't see) I am sure that Constructor (and also Method) is thread-safe. Semantically there should be no difference in using Constructor.newInstance and in invoking the actual constructor explicitly. The latter approach is in any case thread-safe and so should be the former, too.
Concerning "wrongly constructed" instances: As stated above, I'm sure that construction itself will not fail due to concurrency. But you have to consider visibility effects. This means, if a reference is made accessible before construction has been finished (e.g. if you publish "this" from inside the constructor) it may happen that another thread might encounter an inconsistent object state. But again, if such thing happens it has nothing to do with using reflection.
Regards,
Andreas
You would also need to verify that the constructor does not do anything unsafe such as accessing static fields without synchronization.