Prepare Java concurrency interview answers on volatile, atomic updates, locks, executors and cancellation with a shared-counter race example. These original practice questions connect a concept to a decision and a failure case. They are preparation exercises, not leaked employer questions. State your assumptions before proposing an implementation.
How do visibility and atomicity differ?
Visibility concerns whether threads observe the relevant changes under the memory model. Atomicity concerns whether an operation behaves as an indivisible update for the requirement. A visible variable can still participate in an unsafe compound operation. Explain a read-modify-write sequence explicitly rather than treating every assignment and expression as one atomic business action.
Why does volatile not make increment atomic?
An increment requires reading a value, calculating a new value and writing it. Two threads can read the same old value and lose one update even when visibility is established appropriately. Choose an atomic update or locking strategy suitable to the invariant. If the requirement spans several related fields, an atomic counter alone may not make the whole state consistent.
How would you reason about a lock?
Name the state and invariant it protects, then ensure every participating access follows the same discipline. Keep the protected work bounded and avoid holding the lock through unrelated slow calls. When multiple locks are needed, define an acquisition order and consider whether the design can use fewer shared mutable objects. A lock solves neither all application races nor coordination across separate processes.
What does an executor help control?
It provides a way to manage task execution rather than creating threads without an ownership strategy. Explain queue size, concurrency, rejection behavior and shutdown in the actual implementation. A growing queue can delay requests while hiding overload. Choose a capacity policy based on the workload and dependency limits, and define what the caller sees when new work cannot be accepted.
How should cancellation be discussed?
Interruption and cancellation require cooperation from the running code and the operations it calls. Specify the cleanup and outcome when a task stops. Do not silently discard an interrupt and continue indefinite work. A completed external side effect remains completed even if the local task is canceled. Test blocked waiting, running computation and shutdown while tasks are queued.
Worked interview scenario
Original race: a shared counter starts at zero. Thread A reads zero; thread B also reads zero. Both compute one and write one. Two increments occurred, but the final counter is one. Explain the interleaving before naming an API.
Use an appropriate atomic update for this single counter, then change the requirement: the counter and a list of processed IDs must change consistently. Reconsider the invariant and protect the combined operation. Test repeated IDs and concurrent requests. If several service instances share the business rule, describe the durable coordination boundary; a correct in-process lock cannot protect data in every instance.
Practice exercise
Write the two-thread timeline with each read and write. Explain why visibility alone cannot prevent the lost update. Then draw an executor with a bounded queue and define the rejection and shutdown behavior. Prepare one follow-up about preserving cancellation signals through application code.
Review your explanation
Use Cluegent during practice to challenge your own draft. Ask for a follow-up about the scenario's weakest assumption, answer it without suggestions, then check your reasoning against the official reference. Review current plans before subscribing. Follow the employer's tool policy in the actual interview.
Sources checked
These official references support the guide. Product details and technical documentation can change; check the linked source for current information.
Where Cluegent helps
Cluegent supports permitted live workflows with transcript context, typed prompts, screenshot-aware answers, resume context, custom response behavior, quick action buttons, and a private desktop overlay. It is most useful when you already understand the subject and need help staying structured under pressure.
Frequently asked questions
Does volatile make counter++ atomic?
No. Increment is a compound read-modify-write operation and needs the appropriate atomic or locking mechanism.
Can a Java lock coordinate separate service instances?
An ordinary in-process lock cannot. Use the durable shared system's coordination rules where the invariant spans processes.