"in a thread safe singleton does the return have to be inside the synchronized block" Code Answer


does the return really needs to be inside the synchronized block?

no the return does not need to be in the synchronized block unless the singleton field can be assigned elsewhere. however, there is no good reason why the return shouldn't be inside of the synchronized block. if the entire method is wrapped in a synchronized then you can just mark the method as synchronized if we are in the singleton class here. this would be cleaner and better in case singleton gets modified elsewhere.

in terms of why it doesn't need to be inside, since you are using a synchronized block, there is a read-barrier crossed at the start of the block and a write-barrier at the end, meaning that the threads will get the most up-to-date value of singleton and it will only be assigned once.

the read memory barrier ensures that the threads will see an updated singleton which will either be null or a fully published object. the write memory barrier ensures that any updates to singleton will be written to main memory which includes the full construction of singleton and the publishing of it to the singleton field. program order guarantees that the singleton assigned within the synchronized block will be returned as the same value unless there is another assignment in another thread to singleton then it will be undefined.

program order would be more in force if you did something like the following. i tend to do this when singleton is volatile (with appropriate double-check locking code).

synchronized (singleton.class) {
    singleton value = singleton;
    if (singleton == null) {
       value = new singleton();
       singleton = value;
    return value;

not thread-safe because of the return singleton outside the synchronized block

since you are using a synchronized block, this isn't an issue. the double check locking is all about trying to avoid the synchronized block being hit on every operation as you point out.

all the threads running within the synchronized block will force a happen-before relation (i.e., there is no way threads will return null if the instance was properly set) or am i wrong?

that's correct. you aren't wrong.

however, that can be simply because performance-wise it is the same since threads have to synchronized away, so why not be on the safe side and put the return inside?!.

no reason not to although i would argue that the "safe side" is more about causing consternation when others review this code and are worrying about it in the future, as opposed to being "safer" from the standpoint of the language definition. again, if there are other places where singleton is assigned then the return should be inside of the synchronized block.

By Casper Zandbergen on March 21 2022

Answers related to “in a thread safe singleton does the return have to be inside the synchronized block”

Only authorized users can answer the Search term. Please sign in first, or register a free account.