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