Skip to content
Sharp

Java Threading interview questions

Atomic Variables, ExecutorService, Race Conditions, Synchronized and Locks, The Volatile Keyword, Virtual Threads

62 questions with answers and explanations · 62 flashcards

Questions (page 1 of 2)

  1. Which method in ExecutorService is used to submit a Callable task?Multiple choiceEasy
  2. Which method would you use to create a Virtual Thread in Java?Multiple choiceEasy
  3. Which ExecutorService method can submit a Runnable for execution?Multiple choiceEasy
  4. What happens if a task throws an unchecked exception in an ExecutorService?Multiple choiceHard
  5. Which method in ExecutorService can be used to attempt a graceful shutdown, allowing current tasks to complete before termination?Multiple choiceHard
  6. In a high-concurrency e-commerce flash sale, which construct best avoids a race condition when updating inventory levels?Multiple choiceMedium
  7. Why do teams use an ExecutorService instead of manually creating and starting threads in production code?Multiple choiceEasy
  8. Which method in ExecutorService can be used to retrieve results of multiple tasks at once?Multiple choiceMedium
  9. In what scenario might a ReentrantLock be preferred over a synchronized block?Multiple choiceMedium
  10. What tool can be used to monitor the performance and behavior of Virtual Threads in a Java application?Multiple choiceMedium
  11. Which rule is strongest for enforcing inventory correctness across multiple application instances?Multiple choiceHard
  12. How would you explain a key advantage of using Java 21's Virtual Threads in an interview?Multiple choiceHard
  13. Which of the following statements about Virtual Threads is true?Multiple choiceMedium
  14. Where should a slow payment API call usually happen when protecting inventory with a lock?Multiple choiceMedium
  15. How does the 'volatile' keyword help in addressing visibility issues in a multithreaded context?Multiple choiceHard
  16. Which Java class would you use to create a fixed-size thread pool for virtual threads?Multiple choiceMedium
  17. Why does calling the awaitTermination() method on an ExecutorService change runtime behavior?Multiple choiceMedium
  18. In which scenario is ExecutorService preferred over manually managing threads?Multiple choiceEasy
  19. Why can virtual threads make an existing race condition easier to reproduce?Multiple choiceHard
  20. What impact do Virtual Threads have on thread pool management in Java?Multiple choiceHard
  21. What problem do virtual threads primarily solve?Multiple choiceMedium
  22. What will be the output of the following code snippet if executed successfully?Multiple choiceEasy
  23. Why does using volatile keyword in a multi-threaded environment change runtime behavior?Multiple choiceMedium
  24. How do Virtual Threads differ from traditional threads in handling blocking operations?Multiple choiceMedium
  25. Why is using new Thread().start() for each task in a high-traffic application discouraged?Multiple choiceMedium
  26. How would you explain a potential risk of using a single-threaded Executor for processing tasks in a multi-threaded environment in an interview?Multiple choiceHard
  27. What does the ReentrantLock class provide that synchronized blocks do not?Multiple choiceEasy
  28. How does a ConcurrentHashMap handle concurrent updates to ensure thread safety?Multiple choiceHard
  29. How does ConcurrentHashMap differ from a regular HashMap in a multithreaded environment?Multiple choiceMedium
  30. Which scenario is most suitable for using virtual threads?Multiple choiceEasy
  31. How does ExecutorService handle a task if all threads in a FixedThreadPool are busy?Multiple choiceMedium
  32. Which of the following is NOT a direct benefit of using a Thread Pool?Multiple choiceEasy
  33. What characteristic of a ForkJoinPool makes it suitable for divide-and-conquer algorithms?Multiple choiceHard
  34. In what scenario would traditional OS threads still be preferred over Java Virtual Threads?Multiple choiceHard
  35. A local oversell bug disappears after you add synchronized, but production runs four application instances. What risk remains?Multiple choiceMedium
  36. Why do teams use ExecutorService for thread management in a concurrent application in production code?Multiple choiceMedium
  37. When multiple threads increment a shared counter without proper synchronization, why might the final count be incorrect?Multiple choiceMedium
  38. Why is the 'check-then-act' pattern problematic in a concurrent environment?Multiple choiceMedium
  39. How would you explain a key advantage of Virtual Threads in achieving high concurrency in Java applications in an interview?Multiple choiceMedium
  40. Why is it important to use synchronization when accessing shared mutable data across multiple threads?Multiple choiceEasy
  41. Which method in ExecutorService allows submitting a task that can throw checked exceptions?Multiple choiceEasy
  42. In a distributed store, where must the final inventory correctness rule live?Multiple choiceHard
  43. Which statement about AtomicInteger is most accurate?Multiple choiceMedium
  44. Why would you choose to use a Thread Pool in an ExecutorService?Multiple choiceMedium
  45. Why is using a non-synchronized method to update a shared variable in a multithreaded environment a problem in practice?Multiple choiceEasy
  46. How does a FixedThreadPool in ExecutorService help manage high traffic scenarios?Multiple choiceMedium
  47. How does the JVM handle a virtual thread that encounters a blocking I/O operation?Multiple choiceHard
  48. Why might creating a new thread with 'new Thread().start()' for each task be problematic in a high traffic application?Multiple choiceMedium
  49. Why does Java's Virtual Threads introduced in Project Loom help in practice?Multiple choiceEasy
  50. Which characteristic best describes Java Project Loom's virtual threads?Multiple choiceEasy