Skip to content
Sharp

Concurrency basics · interview question

Which changes would make the counter in a multi-threaded increment loop correct? Select all that apply.

Select all that applyMedium
Select all that apply
Answer and explanation

Correct answer

  • A. Use an AtomicInteger and call incrementAndGet().
  • B. Do the increment inside a synchronized block on a shared lock.
  • D. Use a LongAdder and call increment().

Why

Atomics use CAS instructions; synchronized provides mutual exclusion; LongAdder spreads contention across cells and is fastest under heavy write contention.

Test yourself on Concurrency basics

A free test with instant, explained results. No account needed.

Start free test
All Concurrency basics questions →