Java Multithreading – Interview Questions and Answers
1. What is multithreading in Java?
Answer: Multithreading is a Java feature that allows concurrent execution of two or
more threads. Threads share the same memory but execute independently.
2. What is the difference between process and thread?
Answer:
Process: Independent, has its own memory space.
Thread: Lightweight sub-process, shares memory with other threads of the same
process.
3. How do you create a thread in Java?
Answer:
Extending the Thread class and overriding run().
Implementing the Runnable interface and passing it to a Thread object.
Using Callable and Future (for threads that return values).
4. What are the states of a thread?
Answer:
New
Runnable
Blocked
Waiting
Timed Waiting
Terminated
5. What is the difference between sleep(), wait(), and
join()?
Answer:
sleep(): Pauses the thread for specified time (static method).
wait(): Releases the lock and waits until notified (object method).
join(): Waits for another thread to finish.
6. What is synchronization? Why is it needed?
Answer: Synchronization ensures only one thread accesses a critical section of code
at a time. It prevents data inconsistency in concurrent environments.
7. How do you implement synchronization in Java?
Answer:
Using synchronized keyword (method or block).
Using ReentrantLock from java.util.concurrent.locks package.
8. What is the difference between synchronized method
and block?
Answer:
Synchronized method: Locks the entire method.
Synchronized block: Locks only the part of the code, providing better performance.
9. What is a deadlock?
Answer: Deadlock occurs when two or more threads wait indefinitely for each other
to release locks.
10. How can you prevent deadlock?
Answer:
Avoid nested locks.
Lock ordering.
Use timeout for locks.
Use higher-level concurrency tools.
11. What is a daemon thread?
Answer: Daemon threads run in the background and support user threads. JVM exits
when only daemon threads are running (e.g., Garbage Collector).
12. What is ThreadLocal in Java?
Answer: It provides thread-local variables — each thread accessing the variable has
its own isolated copy.
13. What is the difference between volatile and
synchronized?
Answer:
volatile: Ensures visibility of changes to variables across threads.
synchronized: Ensures visibility and also atomicity.
14. What is the Executor framework?
Answer: It is a framework for managing and controlling thread execution using
ExecutorService, ThreadPoolExecutor, etc.
15. What is Callable and Future?
Answer:
Callable: Similar to Runnable but returns a result.
Future: Represents the result of an asynchronous computation.
16. What is the difference between start() and run()
method?
Answer:
start(): Starts a new thread and calls run() internally.
run(): Executes the code in the current thread, no multithreading.
17. What is a race condition?
Answer: Occurs when two or more threads access shared data and try to change it at
the same time.
18. What is Thread pool?
Answer: A pool of worker threads maintained to reuse threads and improve
performance by reducing thread creation overhead.
19. How is inter-thread communication achieved in
Java?
Answer: Using wait(), notify(), and notifyAll() methods on shared objects.
20. What is the difference between notify() and
notifyAll()?
Answer:
notify(): Wakes up one thread waiting on the object's monitor.
notifyAll(): Wakes up all threads waiting on the object's monitor.