~/ learn/ comp-308/ cards/ Shared state and synchronized
1 of 5

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.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/f18ed4fa-9592-4f37-9968-d0069ddd3033/flashcard utf-8 LF