KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Is there any reason to use volatile and synchronized together in this code? public class Helper { private volatile int n; private final Object lock = new Object(); public Helper(int n) { this.n = n; } public void setN(int value) { synchronized (lock) { n = value; } } } Class Helper must be thread safe. I've got this example from the "Java Concurrency Guidelines" book, but it is still not clear: what is the reason for using volatile and synchronized together in this example?
Tags (comma-separated)
Save Edits
Cancel