简体   繁体   中英

Calling a synchronized method from another synchronized method, both on different object

Lets say we have two classes A, B and synchronized methods methodA, methodB of the respective classes. If we call synchronized methodB from synchronized methodA whether the thread still hold lock on ObjectA while methodB is still executing?

Class A
{
   public synchronized void methodA()
   {
       //do something;
       synchronized(ObjectB)
       {
          ObjectB.methodB();
       }
   }
}

Class B
{
   public synchronized void methodB()
   {
      //do something
   }
}

The lock will held as long as you are in sync block. It is allowed to lock as many locks as you need but this nested synchronization can impact the overall performance of your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM