Multithreading and Concurrency - 20 Interview Questions and Answers
1. What is a thread in Java?
A thread is a lightweight subprocess, the smallest unit of processing. Threads share memory and allow
multitasking in a program.
2. How do you create a thread in Java?
1. Extend the Thread class and override the run() method.
2. Implement Runnable and pass it to a Thread.
3. What is the difference between start() and run() methods?
start() creates a new thread and invokes run(). run() just executes on the current thread.
4. What is the lifecycle of a thread?
New -> Runnable -> Running -> Blocked/Waiting -> Terminated.
5. What is synchronization in Java?
Synchronization ensures mutual exclusion-only one thread can access a critical section at a time.
6. What is the difference between process and thread?
A process is independent and has its own memory; threads are parts of the same process and share
memory.
7. What is a race condition?
When multiple threads access and modify shared data concurrently, causing unpredictable results.
Multithreading and Concurrency - 20 Interview Questions and Answers
8. How can you prevent race conditions?
Use synchronized blocks, locks, or atomic classes like AtomicInteger.
9. What is a deadlock?
A condition where two or more threads are blocked forever waiting on each others resources.
10. How to avoid deadlock in Java?
Avoid nested locks, use a lock ordering strategy, or use timeout locks.
11. What is a thread pool?
A managed collection of worker threads used to execute tasks efficiently, like with
Executors.newFixedThreadPool().
12. What is ExecutorService?
An interface in java.util.concurrent to manage and control thread pools.
13. What is the difference between Callable and Runnable?
Callable returns a result and can throw exceptions; Runnable does not return a result.
14. What is Future in Java?
Future represents the result of an asynchronous computation.
15. What are atomic variables?
Atomic variables (e.g., AtomicInteger) support lock-free thread-safe operations on single variables.
Multithreading and Concurrency - 20 Interview Questions and Answers
16. What is the synchronized block?
A synchronized block locks an object for mutual exclusion, reducing scope compared to synchronized
methods.
17. What is ReentrantLock?
A lock with the same locking semantics as synchronized but with more control like tryLock and interruptible
waits.
18. What is ThreadLocal in Java?
ThreadLocal provides thread-confined variables, useful for objects like SimpleDateFormat.
19. What is the volatile keyword?
Volatile ensures visibility and ordering-changes to a variable are visible across threads immediately.
20. What is ForkJoinPool?
A special ExecutorService for parallel computation by dividing tasks into subtasks and joining the results.