When to use volatile keyword vs synchronization in multithreading?
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.
Use
volatileto guarantee that each read access to a variable will see the latest value written to that variable. Usesynchronizedwhenever you need values to be stable for multiple instructions. (Note that this does not necessarily mean multiple statements; the single statement:is not thread-safe even if
varis declaredvolatile. You need to do this:See here for a nice summary of this issue.