I\'m trying to figure out how synchronized methods work. From my understanding I created two threads T1 and T2 that will call the same method <
Each A
instance has its own B
instance. The method addNew
is an instance method of B
. Therefore, the lock acquired implicitly during calls to addNew
is the lock on the receiver B
instance. Each thread is calling addNew
on a different B
, and therefore locking on different locks.
If you want all B
instances to use a common lock, create a single shared lock, and acquire it in the body of addNew
.