Interview Questions

Suppose you have thread and it is in synchronized method and now can thread enter other static synchronized method from that method?

Java Interview Questions and Answers (part 4)


(Continued from previous question...)

47. Suppose you have thread and it is in synchronized method and now can thread enter other static synchronized method from that method?

Yes, here when thread is in synchronized method it must be holding lock on object’s monitor and when it enters static synchronized method it will hold lock on class’s class object as well.

So, now thread holds 2 locks (it’s also called nested synchronization)- first one on object’s monitor.
second one on class’s class object.(This lock will be released when thread exits static method).

(Continued on next question...)

Other Interview Questions