Calling a synchronized method from a synchronized method, both from the same object

Why is the code below not deadlocking? I mean, after calling getNumber (.), The object of the Test class should be locked, so I should not have access to getNumber2 (.).

class Test() {
    synchronized int getNumber(int i){
        return getNumber2(i);
    }

    synchronized int getNumber2(int i) {
        return i;
    }

    public static void main(String[] args) {
        System.out.println((new Test()).getNumber(100));
    }
}

Conclusion:

100
+5
source share
2 answers

This is because the lock is a retry, which means that it can be obtained several times by the same thread.

From the Java tutorial :

Reentrant synchronization

, , . , . , . , , , , , . , .

JLS §17.1. :

Java . , . Java , . . , , , . t ; .

+19

, , , , this, , , .

, this, .

+4

All Articles