synchronized statement for a particular code snippet

Viewed 222

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);
}
  1. 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?

  1. 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.

3 Answers
Related