Reputation: 764
public void addName(String name) {
synchronized(this) {
lastName = name;
nameCount++;
}
nameList.add(name);
doA();
doB();
}
Following the java document's example above, the "nameList.add(name);doA();doB();" are thread-safe ?
Upvotes: 0
Views: 37
Reputation: 7466
No. Only:
synchronized(this) {
lastName = name;
nameCount++;
}
Is shown to be threadsafe here.
doA() and doB() could have additional locking though, but may be called multiple times which could have unintended side effects.
Upvotes: 3