I have the following code snippet from https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html
public void addName(String name) {
synchronized(this) {
lastName = name;
nameCount++;
}
nameList.add(name);
}
- I think
nameList.add(name);also needs to be in synchronized block since nameList content should also be happen-before relationship like Collections.synchronizedList(List).
Any thought on this?
- And it also says
Without synchronized statements, there would have to be a separate, unsynchronized method for the sole purpose of invoking nameList.add.
I don't understand this sentence why nameList.add should be in a separate unsynchronized method if there is no synchronized statements.