Concurrency basics interview questions
Threads, synchronization, volatile, atomics, executors.
110 questions with answers and explanations · 108 flashcards
Key ideas
count++is read-modify-write and not atomic; unsynchronized threads lose updates.synchronizedgives mutual exclusion and visibility.volatilegives visibility and ordering, but not atomicity.- Prefer
java.util.concurrent:AtomicInteger,LongAdder,ConcurrentHashMap,ExecutorService. shutdown()stops accepting tasks and lets queued ones finish;shutdownNow()also interrupts running tasks.
Questions (page 3 of 3)
- Which scenario requires both 'volatile' and synchronization?Multiple choiceHard
- Why is it important to release a ReentrantLock in a finally block?Multiple choiceHard
- What potential issue can ReentrantLock help solve compared to synchronized blocks?Multiple choiceMedium
- How does the synchronized keyword affect the performance of a Java application?Multiple choiceEasy
- Which of the following scenarios would require both 'volatile' and synchronization?Multiple choiceHard
- Why do teams use ReentrantLock over synchronized blocks in a high-concurrency environment in production code?Multiple choiceHard
- What does trying to acquire a lock using ReentrantLock's tryLock() method if the lock is not available evaluate to?Multiple choiceEasy
- What happens if a thread tries to acquire a lock that it already holds using a ReentrantLock?Multiple choiceEasy
- In a multicore system, what role does 'volatile' play regarding CPU cache?Multiple choiceHard
- Why can using synchronized(this) in a static method become a real problem?Multiple choiceMedium