A shared boolean stop flag is updated by one thread and read by another. Which keyword is usually enough for visibility?
Practice this question from Quiz: Java Concurrency, with options and an explanation.
Question
A shared boolean stop flag is updated by one thread and read by another. Which keyword is usually enough for visibility?
Option A — Correct answer
volatile
volatile is a good fit for simple visibility flags.
Option B
transient
Option C
final only after mutation
Option D
package-private
Explanation
volatile makes writes visible to other threads and is suitable for simple stop flags.