Java Interview Questions – Concurrency & Parallel Processing
Q1: [Amazon] What is concurrency in Java?
Answer: Concurrency is the ability of a program to execute multiple tasks simultaneously using multithreading.
Q2: [Google] What is the difference between concurrency and parallelism?
Answer:
- Concurrency: Multiple tasks progress independently (but may not run at the exact same time).
- Parallelism: Multiple tasks execute simultaneously using multiple CPU cores.
Q3: [Microsoft] What is the Executor Framework in Java?
Answer: The Executor Framework provides thread pool management, making it easier to handle multiple tasks efficiently.
Q4: [Netflix] What are the types of thread pools in the Executor framework?
Answer:
- Fixed Thread Pool (Executors.newFixedThreadPool(n))
- Cached Thread Pool (Executors.newCachedThreadPool())
- Single Thread Pool (Executors.newSingleThreadExecutor())
- Scheduled Thread Pool (Executors.newScheduledThreadPool(n))
Q5: [Facebook] What is the difference between synchronized and Lock in Java?
Answer:
- synchronized: Locks the entire method/block and releases automatically.
- Lock (from java.util.concurrent.locks): Provides finer control with explicit locking/unlocking.
Q6: [Oracle] What is a deadlock in Java?
Answer: A deadlock occurs when two or more threads wait indefinitely for resources held by each other.
Q7: [Uber] What is a race condition?
Answer: A race condition occurs when multiple threads modify shared data simultaneously, leading to unpredictable behavior.
Q8: [Deloitte] What is the purpose of the volatile keyword?
Answer: The volatile keyword ensures that changes made by one thread are immediately visible to other threads.
Q9: [JP Morgan] What is ThreadLocal in Java?
Answer: ThreadLocal provides thread-specific storage, ensuring each thread has its own isolated variable instance.
Q10: [Goldman Sachs] How can we avoid deadlocks in Java?
Answer:
- Acquire locks in a fixed order.
- Use timeout-based locks.
- Avoid nested locks.
- Use tryLock() method from ReentrantLock.
