Register Login

Java Multithreading Interview Questions and Answers

Updated Apr 18, 2025

1. What is multithreading in Java?

Multithreading in Java allows multiple threads to execute concurrently within a single program. It helps in efficient CPU utilization and is widely used in tasks such as animations, game development, and server-side applications.

2. What are the advantages of multithreading in Java?

Advantages include better CPU utilization, improved performance, isolation of threads, responsive UIs, and shared memory usage among threads.

3. Difference between multitasking and multithreading in Java?

Multitasking is running multiple programs simultaneously, each with separate memory. Multithreading is running multiple threads in a single program with shared memory. Multithreading has lower overhead and faster communication.

4. What are the different ways of achieving multithreading in Java?

Multithreading can be achieved by extending the Thread class, implementing the Runnable interface, using lambda expressions (Java 8+), or using ExecutorService and Callable (Java 5+).

5. How to run multithreading in Java?

Create a class implementing Runnable, override the run() method, and pass it to a Thread instance. Then call start() on the thread to execute concurrently.

6. What is a deadlock in Java multithreading?

Deadlock occurs when two or more threads are waiting for each other to release resources, causing all of them to be stuck indefinitely.

7. How to avoid a deadlock in multithreading Java?

Avoid nested locks and acquire locks in a consistent order. Use tryLock with a timeout and minimize the synchronized block scope to prevent deadlocks.

8. What is context switching in Java multithreading?

Context switching is the process where the CPU switches from one thread to another. It involves saving and loading thread states and adds some overhead.

9. What is a daemon thread in Java?

Daemon threads are low-priority threads that run in the background. They do not prevent the JVM from exiting once all user threads are finished.

10. Explain race condition in Java multithreading?

A race condition happens when multiple threads access and modify shared data concurrently without proper synchronization, leading to unpredictable outcomes.

11. Difference between wait() and sleep() method?

wait() is from Object class and releases the lock; sleep() is from the Thread class and does not release the lock. wait() is used for inter-thread communication and requires synchronized blocks.

12. Explain thread pool in multithreading?

A thread pool manages a pool of worker threads to execute tasks, reducing the overhead of thread creation. ExecutorService is commonly used to implement thread pools.

13. What is synchronization in multithreading in Java?

Synchronization controls access to shared resources by allowing only one thread to access a block of code at a time, preventing data inconsistency.

14. Explain Shutdown Hook in multithreading?

A shutdown hook is a thread that is invoked when the JVM is shutting down. It is used for cleanup activities like releasing resources or saving state.

15. What happens if we call run() instead of start()?

Calling run() directly will execute the code in the current thread, not in a new thread. Use start() to initiate a new thread.

16. How will run() be called if we call start()?

Calling start() on a Thread internally invokes the run() method in a new thread. Attempting to start a thread more than once results in an IllegalThreadStateException.

17. How to read and write a file using multithreading in Java?

Use separate threads to perform file read/write operations concurrently. For example, each thread can handle a chunk of the file or a different file to improve performance.

18. What are the states of a thread in Java?

A thread in Java can be in one of the following states: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, or TERMINATED. These represent the lifecycle of a thread from creation to termination.

19. What is the lifecycle of a thread in Java?

The thread lifecycle includes the following stages:

New → Runnable → Running → Waiting/Blocked/Sleeping → Terminated.

20. What is the difference between synchronized block and synchronized method in Java?

A synchronized method locks the entire method, while a synchronized block allows more granular control by locking only a section of code, improving performance.

21. What is ReentrantLock in Java? How is it different from synchronized?

ReentrantLock allows more advanced lock control like tryLock(), fairness settings, and interruptible lock waits, which synchronized blocks do not offer.

22. What is volatile keyword in Java and how does it affect multithreading?

The volatile keyword ensures visibility of changes to variables across threads. It prevents threads from caching variables locally.

23. What is the difference between Runnable and Callable in Java?

Runnable does not return a result or throw checked exceptions, while Callable can return a value and throw exceptions.

24. What is Future and how is it used in Java multithreading?

Future is used to retrieve the result of an asynchronous computation. It is returned by ExecutorService’s submit() method.

25. What is ExecutorService in Java? How is it better than manually creating threads?

ExecutorService manages a pool of threads and handles task scheduling. It improves resource management and scalability compared to manually creating threads.

26. How does Java handle thread priority?

Java allows setting thread priority from 1 (MIN_PRIORITY) to 10 (MAX_PRIORITY). Thread scheduling is influenced by these priorities, but is platform dependent.

27. What is thread starvation and how can it be prevented?

Thread starvation occurs when lower-priority threads do not get CPU time. It can be avoided by using fair locks like ReentrantLock(true).

28. What is livelock in Java multithreading?

Livelock happens when threads keep changing state in response to each other but make no progress. Unlike deadlock, threads are still active.

29. What is ThreadLocal in Java? When should it be used?

ThreadLocal provides thread-confined variables, useful when you want each thread to have its own isolated copy of a variable.

30. What is the difference between concurrency and parallelism in Java?

Concurrency is about managing multiple tasks at once (possibly interleaved), while parallelism is about executing multiple tasks simultaneously using multiple processors.

31. What is fork/join framework in Java?

The fork/join framework is used to divide a task into smaller subtasks and process them in parallel. It's efficient for recursive and divide-and-conquer algorithms.

32. What are atomic variables and how do they help in concurrency?

Atomic variables like AtomicInteger provide lock-free, thread-safe operations on single variables using low-level CPU primitives.

33. What are BlockingQueue and ConcurrentHashMap in Java?

BlockingQueue is used for thread-safe queue operations, while ConcurrentHashMap allows concurrent read and thread-safe write operations on a map.

34. What is the synchronized keyword’s role in preventing race conditions?

The synchronized keyword ensures that only one thread can access a critical section at a time, preventing race conditions on shared resources.

35. What is the difference between join() and yield() methods in Java?

join() makes the current thread wait for another to finish. yield() hints the scheduler that the current thread is willing to yield its current use of CPU.

36. What is the use of CountDownLatch and CyclicBarrier?

CountDownLatch waits for a number of threads to complete, while CyclicBarrier allows a group of threads to wait at a common point before proceeding.

37. What are best practices for writing thread-safe code in Java?

Use immutable objects, avoid shared mutable state, use synchronization wisely, prefer higher-level concurrency utilities, and minimize lock scope.


×