Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronization in case of Inheritance in java

Suppose there are 2 classes, Parent class and Child class which extends Parent class. Now and both have two method which are synchronized.My question which object lock will be use for locking this synchronized methods i create object like : Parent p=new Child(); Is is parent object lock or Child object lock?

like image 371
coreJavare Avatar asked Feb 13 '23 12:02

coreJavare


1 Answers

There is no parent object lock or child object lock. There is only one lock, the object's. In this case, it will be p's.

Threads will block when entering a parent's synchronized method if another thread has the lock in a child's synchronized method, and vice versa.

like image 92
Jan Van den bosch Avatar answered Feb 15 '23 11:02

Jan Van den bosch