Say we have something like this,
class Class{
//...
synchronized void m1(Class obj){ obj.m2(); }
synchronized void m2(){ /*...*/ }
}
My question – when is lock of an object obj released? When it returns from method m2, or method m1?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Try to translate the
synchronizedmethods in synchronized blocks:So your
objObject is only locked during them2()call. This is because that’s the only time whenthistranslates toobj.The
m1()call only locks the current object, which by the way could also be the sameobj; if this is the case (this == objin the first call), thenobjis locked until bothm1()andm2()complete.