Sumant Dange
Sumant Dange

Reputation: 33

Does the lock on a static synchronized method affect the non-static synchronized methods of it's instances?

Suppose I have 2 methods, one declared as synchronized and the other declared as static synchronized. So when a thread acquires the class-level lock, does it acquire the locks on all its instances as well? In other words, if a thread acquires a class-level lock, can another thread acquire an object-level lock on one of its instances simultaneously?

Upvotes: 1

Views: 168

Answers (1)

Burak Serdar
Burak Serdar

Reputation: 51657

A static synchronized method will acquire the lock on the Class instance for the class. A synchronized method will acquire the lock on this. When you acquire the class level lock by calling a synchronized static method, the object-level locks are not affected.

Upvotes: 6

Related Questions