Type a synchronized method that makes increment atomic
Type a synchronized method that makes increment atomic
Answer
public synchronized void increment() { count++; }
synchronized acquires this object’s intrinsic lock for the whole method body, so read-modify-write of count cannot interleave with another thread holding the same lock.