Does a static method synchronization delay creation of object of the class?

class A{
    synchronized static void method(){
        doSomethingLongTime(); // here A.class monitior is taken.
    }
}

…….

new A(); // does this blocked by doSomethingLongTime() ?

The code above depicts my question: new A() definetly deal with A.class, so is it blocked?

Answer

Nope. The lock on the static method is acquired on A class object. That is right. But new A() is not inside synchronized block. So this line doesn’t need to wait for any lock object & can proceed. Construction of new object won’t be blocked otherwise explicitly specified within a synchronized block.

Leave a Reply

Your email address will not be published. Required fields are marked *