Skip to content

Why are we using synchronized twice in com.iluwatar.singleton.ThreadSafeLazyLoadedIvoryTower? #2459

@hmittal657

Description

@hmittal657

`/**

  • The instance doesn't get created until the method is called for the first time.
    */
    public static synchronized ThreadSafeLazyLoadedIvoryTower getInstance() {
    if (instance == null) {
    synchronized (ThreadSafeLazyLoadedIvoryTower.class) {
    if (instance == null) {
    instance = new ThreadSafeLazyLoadedIvoryTower();
    }
    }
    }
    return instance;
    }`

Shouldn't we remove synchronized from method signature? I didn't find any explaination for this. Usually for lazy initialization we have synchronized only inside the method right?
What pros/cons are there for using synchronized in method signature?
Code reference

Metadata

Metadata

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions