Concurrency basics · interview question
Which method would you use to update an AtomicInteger only if it currently holds a specific value?
Multiple choiceMediumchapter-15-curatedpremiumconcurrencyatomic-variables
Answer and explanation
Correct answer
B. CompareAndSet(int expect, int update)
Why
The compareAndSet(int expect, int update) method updates the value of an AtomicInteger only if it currently equals the expected value, providing a way to perform conditional updates atomically.
Test yourself on Concurrency basics
A free test with instant, explained results. No account needed.
More Concurrency basics questions
- 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?
- In which scenario would you use.thenCompose() over.thenApply() in CompletableFuture?
- Which CompletableFuture method should be used to handle both results and exceptions in a unified way?
- How does AtomicInteger's getAndIncrement method differ from incrementAndGet?