Top Java Multithreading Interview Questions and Answers

Multithreading is one of the most critical topics in Java for interviews. Below are the most commonly asked questions and answers related to Java threads, synchronization, and concurrency utilities.

1️⃣ What is a thread in Java?

A thread is a lightweight subprocess, the smallest unit of execution. Java supports multithreading to perform multiple tasks simultaneously.

2️⃣ How do you create a thread in Java?

There are two ways:


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

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

3️⃣ Difference between start() and run()?

  • start() creates a new thread and calls run() in that thread.
  • run() is just a normal method, runs in the current thread.

4️⃣ What is the lifecycle of a thread?

New → Runnable → Running → Blocked/Waiting → Dead

5️⃣ What is synchronization?

Synchronization prevents race conditions by allowing only one thread to access a shared resource at a time.


public synchronized void increment() {
    count++;
}

6️⃣ Difference between synchronized method and synchronized block?

  • Method: Locks the entire object.
  • Block: You can synchronize only the required code.

7️⃣ What is volatile keyword?

Ensures visibility of changes to variables across threads. It does not ensure atomicity.

8️⃣ What is a deadlock?

Deadlock occurs when two or more threads wait indefinitely for each other to release locks.

9️⃣ How to avoid deadlock?

  • Lock ordering
  • Try to use a timeout
  • Use concurrent packages

🔟 What is ExecutorService in Java?

ExecutorService is an interface to manage a pool of threads. Use it to handle asynchronous tasks.


ExecutorService executor = Executors.newFixedThreadPool(5);
executor.submit(() -> {
    System.out.println("Task executed");
});
executor.shutdown();

1️⃣1️⃣ What is Callable and Future?

Callable returns a result and can throw an exception. It is used with Future to get the result later.

1️⃣2️⃣ Difference between wait() and sleep()?

  • wait() releases the lock, used for inter-thread communication.
  • sleep() pauses the thread, but holds the lock.

1️⃣3️⃣ What is CountDownLatch?

It allows one or more threads to wait until a set of operations being performed in other threads completes.

📌 Conclusion

Multithreading helps improve performance but requires careful handling of shared resources. Mastering these concepts will make you stand out in Java interviews!

Also read about real-world concurrency techniques in Concurrency and Multithreading in Java.