0
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?

J.J. Beam
  • 2,612
  • 2
  • 26
  • 55

1 Answers1

1

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.

aatwork
  • 2,130
  • 4
  • 17
  • To add: I've heard multiple times that object instantiation (new) is synchronized anyway. Synchronized on static methods always attaches to the defining Class. Creating objects will NOT stumble over that synchronization, UNLESS you tell you code to do so in the CTOR – JayC667 Mar 14 '21 at 20:40