What does intrinsic lock actually mean for a Java class?

前端 未结 7 1539
逝去的感伤
逝去的感伤 2021-02-04 10:50

In order to properly understand the issues and solutions for concurrency in Java, I was going through the official Java tutorial. In one of the pages they defined Intrin

7条回答
  •  一个人的身影
    2021-02-04 11:22

    It doesn't matter whether the synchronized method belongs to the same class or not, what matters is if the caller thread of the method acquires the lock or not, if it does, then it will be allowed enter the critical section because the lock is reentrant.

    If it wasn't the case, then a recursive call would cause a deadlock,

    fn(){
     synchronized(mutex){ // the current thread has already acquired the mutex
       fn();
     }
    }
    

    fn here wont deadlock because the lock is re-entrant, ie ( the thread that's already acquiring the lock can enter and renter the critical section again as long as it is still acquired).

提交回复
热议问题