Concurrency basics · interview question
What does declaring a field volatile guarantee?
Multiple choiceMedium
Answer and explanation
Correct answer
A. Writes are visible to other threads, with ordering guarantees, but compound actions like count++ are still not atomic.
Why
volatile establishes a happens-before edge between a write and later reads. It's great for flags like running, but not for counters.
Test yourself on Concurrency basics
A free test with instant, explained results. No account needed.
More Concurrency basics questions
- Which changes would make the counter in a multi-threaded increment loop correct? Select all that apply.
- Which ExecutorService method stops accepting new tasks but lets already-submitted tasks finish? (method name)
- Which method is used to wait for all CompletableFutures in a list to complete?
- How can you ensure that a CompletableFuture runs on a specific Executor?
- Which situation benefits most from using AtomicInteger over synchronized blocks?
- Which CompletableFuture method should be used to execute a task asynchronously in a non-blocking manner?