🎴 Flashcard Mode
Java Programming Quiz
Card1 / 20
Mastered0
Review0
QuestionClick to flip
Given: public class NamedCounter { private final String name; private int count; public NamedCounter(String name) { this.name = name; } public String getName() { return name; } public void increment() { count++; } public int getCount() { return count; } public void reset() { count = 0; } } Which three changes should be made to adapt this class to be used safely by multiple threads? (Choose three.)
AnswerClick to flip back
A
declare reset() using the synchronized keyword
B
declare getCount() using the synchronized keyword
C
declare increment() using the synchronized keyword
💡 Explanation:
The count field is mutable and accessed by reset(), getCount(), and increment(). These need synchronization to prevent race conditions. getName() only reads immutable 'name', and constructors don't need sync as the object isn't shared yet.