Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extended classes synchronized methods locking

Suppose this code

class A
{
    public synchronized void methodA()
    {
        // ...
    }
}

class B extends A
{
    @Override
    public synchronized void methodA()
    {
        // ...
        super.methodA();
    }
}

What lock should be acquired by any thread if it wants to access methodA function of class B and methodA of super class A by super.methodA()?

like image 618
user2653926 Avatar asked Jan 28 '14 07:01

user2653926


1 Answers

When you call B b = new B(); b.methodA(); current thread will aquire lock on b , enter B.methodA and call A.methodA. Both methods share the same object and when entering A.methodA the thread will just re-enter the same lock which it already owns.

like image 173
Evgeniy Dorofeev Avatar answered Oct 05 '22 02:10

Evgeniy Dorofeev