A shared counter uses count++ under heavy concurrency. Which replacement is usually safer without writing synchronized code?
Practice this question from Quiz: Java Concurrency, with options and an explanation.
Question
A shared counter uses count++ under heavy concurrency. Which replacement is usually safer without writing synchronized code?
Option A — Correct answer
AtomicInteger.incrementAndGet()
AtomicInteger provides atomic counter operations.
Option B
String.toUpperCase()
Option C
Arrays.asList()
Option D
Scanner.nextLine()
Explanation
AtomicInteger provides atomic increment operations such as incrementAndGet.