Let’s say I have this java code:
synchronized(someObject)
{
someObject = new SomeObject();
someObject.doSomething();
}
Is the instance of SomeObject still locked by the time doSomething() is called on it?
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.
The thread will still own the monitor for the original value of
someObjectuntil the end of the synchronized block. If you imagine that there were two methods,Monitor.enter(Object)andMonitor.exit(Object)then the synchronized block would act something like:From section 14.19 of the JLS:
Note how the evaluation only occurs once.