Java Concurrency – Can Multiple Synchronized Methods Run Concurrently?

concurrencyjavamultithreadingsynchronization

As far as i know when there are multiple synchronized methods(both static and instance) in a class, java allows only one such method to run at a time. But what if a thread acquires lock on an object instance(or class), then enters a synchrozed method inside that object and then makes a call to another synchronized method of the same object. I mean to say:

Class AA  
{  
 ..  
 ..  
synchronized void X()  
{  
  Y();  
}

synchronized void Y()  
{  
 ..  
 ..  
}  

Is any thread execting the method X going to block forever? Since java wont allow to run both method X and Y at the same time?

Best Answer

In Java, synchronized locks are re-entrant.

Or in other words if your thread already holds the lock on an object it doesn't have to wait on itself.