RFR: 7357: Double-Checked Locking in Agent Plugin code [v2]
Guru Hb
ghb at openjdk.java.net
Mon Jul 26 14:22:12 UTC 2021
On Mon, 26 Jul 2021 13:48:12 GMT, Marcus Hirt <hirt at openjdk.org> wrote:
>> application/org.openjdk.jmc.console.agent/src/main/java/org/openjdk/jmc/console/agent/manager/model/PresetRepositoryFactory.java line 53:
>>
>>> 51: if (singleton == null) {
>>> 52: synchronized (PresetRepositoryFactory.class) {
>>> 53: singleton = create();
>>
>> Actually, singleton field need also to be `volatile`.
>> see https://shipilev.net/blog/2014/safe-public-construction/ for more info
>
> Indeed. I haven't checked, but couldn't all this be avoided by simply creating the singleton at the field declaration - does it really have to be lazily created?
We could have created a Singleton in field declaration if below points were true :
1. If we had only "PresetRepository" as a main class with Singleton (Currently this is a factory).
2. Likelihood of "PresetRepository" object required all the time of JMC's lifecycle .
+ "- does it really have to be lazily created?"
3. Singleton Object + init (PresetRepository.addLocalPresetTo(...) ) is doing a File Operation.
-------------
PR: https://git.openjdk.java.net/jmc/pull/282
More information about the jmc-dev
mailing list