Top 15 Java Multithreading Interview Questions – With Real-Life Examples

Top 15 Java Multithreading Interview Questions – With Real-Life Examples




Multithreading is a powerful feature in Java that allows concurrent execution of two or more threads. It helps in maximizing CPU utilization and building high-performance applications. In backend interviews, especially for product-based companies, questions related to Java concurrency and multithreading are asked frequently. Here are 15 top multithreading questions with real-world examples and answers you must prepare for interviews in 2025.

1. What is the difference between a process and a thread?

Process: A process is an independent program in execution with its own memory space and resources.
Thread: A thread is a lightweight sub-process. Multiple threads of a process share the same memory and can communicate easily with each other.

2. How do you create a thread in Java?

There are two common ways:

// 1. Extending Thread class
class MyThread extends Thread {
    public void run() {
        System.out.println("Thread is running");
    }
}
new MyThread().start();

// 2. Implementing Runnable interface
class MyRunnable implements Runnable {
    public void run() {
        System.out.println("Runnable is running");
    }
}
new Thread(new MyRunnable()).start();

3. What is the lifecycle of a thread?

A thread goes through five states:

  • New: Thread is created but not yet started.
  • Runnable: Thread is ready to run and waiting for CPU.
  • Running: Thread is executing.
  • Blocked/Waiting: Thread is paused, waiting for resources or notification.
  • Terminated: Thread has finished execution or terminated due to error.

4. Difference between start() and run() methods?

start() creates a new thread and then calls run() internally. run() is a normal method and if you call it directly, no new thread is started – it runs on the main thread.

5. What is synchronization in Java?

Synchronization prevents thread interference and ensures that only one thread can access critical sections at a time. This avoids data inconsistency.

public synchronized void update() {
    // critical code
}

6. What is a deadlock in multithreading?

Deadlock is a situation where two or more threads wait for each other to release resources, and none proceeds. For example:

  • Thread A locks Resource 1 and waits for Resource 2.
  • Thread B locks Resource 2 and waits for Resource 1.

7. What is the use of volatile keyword in Java?

volatile ensures that changes made by one thread are visible to other threads. It prevents threads from caching variables locally.

8. What is the difference between synchronized and ReentrantLock?

synchronized is a keyword that is easier to use but less flexible. ReentrantLock is an advanced API that gives more control like tryLock(), lockInterruptibly(), fairness policies, etc.

9. What is thread starvation?

Starvation happens when low-priority threads are unable to gain CPU access because high-priority threads are continuously running.

10. How does thread communication work in Java?

Java provides wait(), notify(), and notifyAll() for thread communication.

synchronized (lock) {
    while (!condition) {
        lock.wait();  // waits until notified
    }
    // do something
    lock.notify();  // notifies one waiting thread
}

11. Difference between wait() and sleep()

  • wait() releases the monitor/lock while waiting; sleep() does not.
  • wait() must be used in synchronized blocks; sleep() can be used anywhere.

12. What are daemon threads?

Daemon threads are background threads that run in the background to provide services. They are terminated automatically when all user threads finish execution.

Thread t = new Thread(() -> {});
t.setDaemon(true);
t.start();

13. What is ThreadLocal in Java?

ThreadLocal provides thread-confined variables. Each thread using a ThreadLocal variable has its own, independently initialized copy of the variable.

ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(() -> 0);
threadLocal.set(100);  // this value is unique to the current thread

14. What is Callable and Future?

Callable is like Runnable but it can return a result and throw exceptions.
Future represents the result of an asynchronous computation.

ExecutorService executor = Executors.newSingleThreadExecutor();
Future<String> future = executor.submit(() -> "Result");
System.out.println(future.get()); // prints: Result

15. How to avoid concurrency issues in Java?

  • Use synchronized methods/blocks wisely
  • Use concurrent collections (like ConcurrentHashMap)
  • Use atomic classes (like AtomicInteger)
  • Use ThreadLocal to isolate state per thread
  • Design for immutability

Conclusion

Java multithreading is a must-know topic for backend interviews, especially when you’re targeting product companies or roles that require handling real-time traffic and scalability. These 15 questions cover everything from basic concepts to advanced interview scenarios.

Stay connected with InterviewYatra.com for more Java interview prep, real company questions, and backend system design content.

Post a Comment

0 Comments