intermediateMultithreading & Concurrency280 of 436
volatile vs synchronized: when would you use volatile?
Tests whether you know volatile only guarantees visibility (reads always see the latest write) and prevents instruction reordering around it, but gives no atomicity for compound operations -- so it's right for simple flags/status fields, not for counters or anything needing read-modify-write.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a responseNext Step
←PreviousContinue to synchronized & Monitors — intrinsic locks, owner/entry set/wait set, reentrant semantics→
← Back to all Core Java questions