Does a variable that is accessed by multiple threads, but only inside synchronized blocks, need the volatile modifier? If not, why?
Does a variable that is accessed by multiple threads, but only inside synchronized blocks,
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.
You do not need to use
volatileinside ofsynchronized, synchronized already guarantees the correct behavior for local caching of variables when used consistently (on every access).volatileworks on primitive values, and can be a nice shortcut for atomic accesses to a primitive type. Note that the behavior of volatile has changed in JDK 5 from 1.4.More information can be found here