AtomicInteger and LongAdder counters
java.util.concurrent gives you thread-safe building blocks so you rarely need your own locks: atomic variables (AtomicInteger, LongAdder), concurrent collections (ConcurrentHashMap, CopyOnWriteArrayList, BlockingQueue) and synchronizers (CountDownLatch, Semaphore, CyclicBarrier).
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Use a CountDownLatch so main waits for three worker threads, each adding 100 to a shared AtomicInteger.
Explain it without notes
How does an AtomicInteger increment without a lock?
Why can get-then-put on a ConcurrentHashMap still lose updates, and what should you use?
When would you use CountDownLatch, Semaphore and CyclicBarrier?
Expected output
AtomicInteger: 40000
LongAdder: 40000
CAS succeeds only if unchanged: true, now 0