Can any one tell me the advantage of synchronized method over synchronized block with an example?
Can any one tell me the advantage of synchronized method over synchronized block with
Share
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.
There is not a clear advantage of using synchronized method over the block.
Perhaps the only one ( but I wouldn’t call it an advantage ) is you don’t need to include the object reference
this.Method:
Block:
See? No advantage at all.
Blocks do have advantages over methods though, mostly in flexibility because you can use another object as lock whereas syncing the method would lock the entire object.
Compare:
vs.
Also if the method grows you can still keep the synchronized section separated: