Top 30+ Latest Java Multithreading Interview Questions

This post contains mostly asked Java Multithreading Interview Questions. In Java, the multithreading concept is one of the most important areas to ask questions in the interview therefore we have collected many questions and answers on Java multithreading which are frequently asked in the interview. Since it become a long article so we divided it into different parts. If you are searching for all the questions related to Java Multithreading then you can find them from the given below links.

Java Multithreading Interview Questions

Now, let us see the top Java Multithreading Interview Questions with answers.

1. What is Thread in Java?

Thread is an independent flow of execution. It executes the method in sequence one after one. In a single JVM, we can create multiple threads to have multiple independent flows of execution paths to execute multiple tasks at a time concurrently. Using threads, we can perform complicated tasks in the easiest way. It is considered the simplest way to take advantage of multiple CPUs available in a machine. They share the common address space and are independent of each other.

Suppose there are 2 independent jobs to perform then 2 threads are required. Both threads are going to run simultaneously to perform these 2 jobs simultaneously. Within less time overall job will be completed.

2. What is the difference between thread and process?

Process: It refers to a program that is in execution i.e., an active program. A process can be handled using PCB (Process Control Block).

Thread: It refers to the smallest units of the particular process. It has the ability to execute different parts (referred to as thread) of the program at the same time.

See more Java multithreading interview questions on Introduction to Multithreading:- Part-1:- Introduction – Java Multithreading Interview Questions

3. What are the different ways to define a thread in Java?

We can define a thread in the following 2 ways,

  • By extending Thread class.
  • By implementing Runnable interface.

Defining a thread by extending Thread class,

// defining a thread extending from Thread class
class MyThread extends Thread {
   // override run() method
   @Override
   public void run() {
      // code
   }
}
// Testing
public class ThreadDemo {
   public static void main(String[] args) {
      // thread instantiation
      MyThread t = new MyThread();
      t.start(); // child thread start
   }
}

Defining a thread by extending Thread class,

// defining a thread by implementing Runnable interface
class MyThread implements Runnable {
   // implement run() method
   @Override
   public void run() {
      // code
   }
}
// Testing
public class ThreadDemo {
   public static void main(String[] args) {
      MyThread mt = new MyThread();
      MyThread t = new MyThread(mt);
      t.start(); // child thread start
   }
}

4. Among these two ways of defining a thread, which is better and recommend approach?

Defining a thread by implementing the Runnable interface is better and recommended to use. In the first approach (by extending Thread class) our class is already extended from the Thread class therefore there is no chance of extending it from any other class. In this way, we will miss inheritance benefits.

In a second way (by implementing Runnable interface) our class can extend from any class. And due to this reason, we will have inheritance benefits. Therefore in these two ways, the 2nd way i.e. “implementing from Runnable interface” is recommended. Other than this reason the 1st way also gives performance issues.

5. What is difference between t.start() and t.run()?

MyThread t = new MyThread();
t.start();
t.run();

In the case of the t.start() method, a separate thread will be created which will be responsible for the execution of the run() method. But in the case of the t.run() method, it will be a normal method call. A new thread will not be created, and the run() method will be executed by the main thread itself.

6. What happens if we override start() method?

class MyThread extends Thread {
   public void start() {
      // code
   }
   public void run() {
      // code
   }
}

Since we are overriding the start() method in MyThread class, therefore the start() method of the Thread class will not get a chance to execute. In this case, the start() method of MyThread will be executed like a normal method call. A new thread will not be created. Complete execution will be done by only the main thread, therefore output will be fixed.

7. After Starting the Thread if we are restarting the same thread then what happen?

class MyThread extends Thread {
   public void run() {
      // code
   }
}
public class ThreadDemo {
   public static void main(String[] args) {
      MyThread t = new MyThread();
      t.start(); 
      // some code
      t.start(); 
   }
}

After starting a thread if we are trying to restart the same thread then we will get runtime exception:- java.lang.IllegalThreadStateException. Hence we can’t start a thread more than once.

8. What is context switching?

In Context switching the state of the process (or thread) is stored so that it can be restored and execution can be resumed from the same point later. Context switching enables multiple processes to share the same CPU.

See more Java multithreading interview questions on creating, starting a thread:- Part-2:- Defining a thread, run(), start(), getState(), isAlive(), setName(), getName() Java Multithreading Interview Questions

9. What are the different ways to pause thread Execution?

Different ways to prevent or stop a thread execution for temporarily (not permanently) are,

  • yield() method
  • join() method
  • sleep() method

10. What is the purpose of yield() method?

The yield() method pauses the currently executing thread to give the chance for waiting threads of the same priority.

  • If there is no waiting threads or all waiting threads have low priority then same thread can continue its execution.
  • If multiple threads are waiting with same priority then which waiting thread will get a chance:- we can’t expect, it depends on thread scheduler.
  • The thread which is yield, when it will get the chance back it also depends on thread scheduler and we can’t expect exactly.

11. Difference between yield() and join() method?

If a thread wants to pause its execution to give the chance for remaining threads of the same priority then the yield() method is used. But if a thread wants to wait until the completion of some other thread then we should go for the yield() method.

The yield() method doesn’t contain any overload form and doesn’t throw any Exception whereas the join() method contains three overloaded forms, and all of them throw InterruptedException.

The yield() method is native and static in nature whereas the join() method is final, and instance in nature (non-native).

12. When we should use sleep() method?

If a thread doesn’t want to perform any operation for a particular amount of time then we should use the sleep() method.

See more Java Multithreading interview questions on Preventing thread from execution:- Part-3:- Thread Priority, yield(), join(), sleep() & interrupt() Java Multithreading Interview Questions

13. What is synchronized keyword and where we can apply it?

The synchronized is a modifier applicable for methods and blocks but not for class, objects, and variables. Using the synchronized keyword we can implement the Synchronization concept in multithreading.

14. What is the main advantage of synchronized keyword?

The main advantage of the synchronized keyword:- We can resolve data inconsistency problems.

If multiple threads are operating simultaneously on the same Java object then there will be a chance of data inconsistency problem. The synchronization concept is given to resolve data inconsistency problems and the synchronization concept is implemented by using synchronized keyword.

15. What is the main disadvantage of synchronized keyword?

Since every thread has to execute synchronized method/block one by one therefore It increases the waiting time of threads and creates performance issues.

16. What is race condition?

If multiple threads are operating simultaneously on the same Java object then there will be a chance of data inconsistency problem. This data inconsistency problem in Java is also known as the race condition. To resolve race conditions we should use a synchronized keyword/modifier.

17. What is meaning of thread-safe & non-thread safe objects?

If an object is not accessible for multiple threads concurrently for modifying values, or one thread modification on the object is not affected to another thread, then that object is called a thread-safe object.

The non-thread-safe objects contain opposite of the this behavior. Non-thread-safe objects are accessible for multiple threads concurrently for modifying values. Hence one thread modification on the object is affected to another thread.

When multiple threads are working on the same data, and the value of our data is changing, that scenario is not thread-safe and we will get inconsistent results. When a thread is already working on an object and preventing another thread to work on the same object, this process is called Thread-Safety.

18. Difference between class level lock and object level lock?

If a thread wants to execute a static synchronized method then the class level lock is required. Whereas if a thread wants to execute an instance synchronized method then object level lock is required.

Every object in java has a unique lock. If a thread wants to execute the synchronized method on the given object then first, it has to get a lock-in that object. Once the thread got the object level lock then it is allowed to execute any synchronized method on that object.

Every class in Java has a unique lock. If a thread wants to execute a static synchronized method, then the thread requires a class-level lock. Once a thread got the class level lock, then it is allowed to execute any static synchronized method of that class.

19. What is difference between synchronized methods and blocks?

If the method is declared as synchronized then that method’s complete logic is executed in sequence from multiple threads by using the same object. For different objects, it is executed concurrently from multiple threads. If we declare block as synchronized, only the statements written inside that block are executed sequentially but not the complete method logic.

Using the synchronized method we can only lock the current object of the method. Using synchronized block we can lock the current object, argument object of the method, or any class.

See more Java Multithreading interview questions on Synchronization:- Part-4:- Synchronization in Java Multithreading Interview Questions

20. How two threads can communicate with each other?

Two threads can communicate with each other by using the following methods:-

  • wait()
  • notify()
  • notifyAll()

21. The wait(), notify(), and notifyAll() methods are present in which class?

The wait(), notify(), and notifyAll() methods are defined in java.lang.Object class.

22. Why wait(), notify(), and notifyAll() methods are present in Object class?

The wait(), notify(), and notifyAll() methods are present in Object class but not in Thread class or Runnable interface because a thread can call these methods on any Java object. To call a method on any object the method must be present in that class or parent class. Since java.lang.Object is the supper class of all Java classes therefore they are defined in the java.lang.Object class.

23. When waiting thread got notification then in which state it will go?

When the thread got a notification then go to another waiting state to get the lock of the object. Once the thread gets the lock of the object then it can continue its execution.

24. What is the difference between notify() and notifyAll() method?

We can use notify() method to give the notification for only one waiting thread. If multiple threads are waiting to get the notification then only one thread will be notified, the remaining threads have to wait for further notifications. We can’t expect which thread will be notified, it completely depends upon the JVM and Thread Scheduler. We can use notifyAll() to give the notification for all waiting threads on a particular object.

Java Multithreading Interview Questions

25. What is deadlock in Java?

If two threads are waiting for each other to complete their execution then such type of infinite waiting situation is called deadlock. Synchronization is the only reason for the deadlock situation, hence while using the synchronized keyword we must be careful because the wrong implementation could lead to the deadlock situation. See more:- Program to demonstrate deadlock in Java.

26. What is the keyword causes deadlock?

The synchronized keyword. Deadlock occurs only because of the synchronized keyword.

If we don’t use the synchronized keyword then we never encounter the deadlock situation. In some cases, the program may reach into the infinite waiting state but it is not a deadlock situation. See more:- How to detect deadlock in Java.

27. What is Starvation in Java?

Starvation in Java describes a situation where a thread is unable to gain regular access to shared resources and is unable to make progress. This happens when shared resources are made unavailable for long periods by “greedy” threads. 

28. What is the volatile keyword in Java?

Volatile keyword is used in multithreaded programming to achieve thread safety, as a change in one volatile variable is visible to all other threads so one variable can be used by one thread at a time.

The volatile variable is basically a keyword that is used to ensure and address the visibility of changes to variables in multithreaded programming. This keyword cannot be used with classes and methods, instead can be used with variables. It is simply used to achieve thread safety. If we mark any variable as volatile, then all the threads can read its value directly from the main memory rather than CPU cache, so that each thread can get an updated value of the variable.

29. What is Daemon thread and non-daemon/user thread in Java?

Daemon Thread:- A thread that is running in the background based on a schedule to provide services to non-daemon threads is called daemon thread. Daemon threads are also called service threads. Examples of daemon thread in Java are:- garbage collector, attach listener, signal dispatcher, and e.t.c.

Non-daemon or User Thread:- A thread that executes the main logic of the program/project is called a non-daemon thread. Example of non-daemon thread:- main thread. See more:- Daemon and Non-daemon Thread in Java

30. What is green thread?

The thread which is managed completely by the JVM without taking underlying operating system support is called the green thread. Most of the operating systems won’t provide support for the green thread model. Very few operating systems like Sun Solaris provide support for the green thread model.

See more Java Multithreading interview questions on inter-thread communication:- Part-5:- Inter-thread Communication, Deadlock & Starvation, Daemon Thread Java Multithreading Interview Questions

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *