Four threads increment one counter
QuizMediumJava onlyAsked in: Accenture, Capgemini, Cognizant, Infosys, TCS
Four threads each run count++ 100,000 times on a shared static int count. Which statements are true? Pick all that apply.
- The final value can be less than 400,000
- Making the field
volatilefixes it - Using an
AtomicIntegerandincrementAndGet()gives exactly 400,000 - Doing the increment inside a
synchronizedblock on one shared lock gives exactly 400,000
This question is part of Pro+
Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.