Skip to content
Sharp

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.
  • synchronized gives mutual exclusion and visibility. volatile gives 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)

  1. Which scenario requires both 'volatile' and synchronization?Multiple choiceHard
  2. Why is it important to release a ReentrantLock in a finally block?Multiple choiceHard
  3. What potential issue can ReentrantLock help solve compared to synchronized blocks?Multiple choiceMedium
  4. How does the synchronized keyword affect the performance of a Java application?Multiple choiceEasy
  5. Which of the following scenarios would require both 'volatile' and synchronization?Multiple choiceHard
  6. Why do teams use ReentrantLock over synchronized blocks in a high-concurrency environment in production code?Multiple choiceHard
  7. What does trying to acquire a lock using ReentrantLock's tryLock() method if the lock is not available evaluate to?Multiple choiceEasy
  8. What happens if a thread tries to acquire a lock that it already holds using a ReentrantLock?Multiple choiceEasy
  9. In a multicore system, what role does 'volatile' play regarding CPU cache?Multiple choiceHard
  10. Why can using synchronized(this) in a static method become a real problem?Multiple choiceMedium