Java Multithreading interview questions and answers

In this tutorial, we are going to see Multitheading interview questions with answers.
Here is list of Multithreading interview questions.

1. What is thread in java?

Thread can be called as light weight process. It can be referred as smallest part of process which can be executed concurrently with other parts(threads) of process.

2. What is Multithreading?

Multithreading is execution of multiple threads concurrently. Java supports multithreading , so it allows your application to perform two or more task concurrently. Multithreading can be of advantage specially when now a days, machine has multiple CPUs, so multiple tasks can be executed concurrently.

3. What are ways to create a thread in java?

There are two ways to create a thread in java

  • By extending thread class
  • By implementing Runnable interface.

You can read about more at Java thread example.

4. Thread vs Runnable which is better approach to create a thread?

Implementing Runnable interface is considered to be better approach than Extending Thread due to following reasons.
  • Java does not support multiple inheritance so if you extend Thread class and you can not extend any other class which is needed in most of the cases.
  • Runnable interface represents a task and this can be executed with help of Thread class or Executors.
  • When you use inheritance, it is because you want to extend some properties of parent, modify or improve class behavior. But if you are extending thread class just to create thread, so it may not be recommended behavior for Object Oriented Programming.

5. What are differences between thread and process?

You can go through difference between process and thread to see the differences.

6. What are differences between Sleep and wait in java?


wait should be called from synchronized context i.e. from block or method, If you do not call it using synchronized context, it will throw IllegalMonitorStateException

It need not be called from synchronized block or methods
Calls on

wait method operates on Object and defined in Object class

Sleep method operates on current thread and is in java.lang.Thread
Release of lock
wait release lock of object on which it is called and also other locks if it holds any
Sleep method does not release lock at all
Wake up condition
until call notify() or notifyAll() from Object class
Until time expires or calls interrupt()
wait is non static method
sleep is static method

You can refer difference between sleep and wait in java for more details.

7. Define states of thread in java?

There are 5 states of thread in java
New : When you create a thread object and it is not alive yet.
Runnable:  When you call start method of thread, it goes into Runnable state. Whether it will execute immediately or execute after some times , depends on thread scheduler.
Running : When thread is being executed, it goes to running state.
Blocked : When thread waits for some resources or some other thread to complete (due to thread’s join), it goes to blocked state.
Dead: When thread’s run method returns, thread goes to dead state.

8. Can we call run method directly to start a thread?

No, you can not directly call run method to start a thread. You need to call start method to create a new thread. If you call run method directly , it won’t create a new thread and it will be in same stack as main.
You can refer can we call run method directly to start a thread for more details

9. Can we start a thread twice in java?

No, Once you have started a thread, it can not be started again. If you try to start thread again , it will throw IllegalThreadStateException.
You can refer can we start thread twice for more details

10. How to make a main thread wait until all other threads finished execution?

You can make use of join method to achieve above scenario.
You can read more about join method.

11. What are daemon threads?


Daemon threads are low-priority background threads which provide services to user threads. Its life depends on user threads. If no user thread is running then JVM can exit even if daemon threads are running. JVM does not wait for daemon threads to finish.

12. How can you change user thread to daemon thread?

Answer:setDaemon method can be used to mark thread as user thread. If you put setDaemon(true), it makes thread as daemon.

13. What is Synchronization?


Synchronization is ability to restrict access to shared resource to only one thread. When two or more threads need access to shared resource, there has to be some mechanism such that shared resource will be used by only one thread. The process by which we can achieve it is called Synchronization.

14. What is need of Synchronization?

Let’s understand this with the help of an example.

Let’s say you want to count number of request you got for a particular URL. If you get two requests at the same time, then count may be inconsistent.
Without Synchronization:

For example:
Thread T1 sees count as 20 and increment it to 21. At the same time, thread t2 also sees count as 20 and increment it to 21. This shows that count became inconsistent.

With Synchronization:

You can achieve Synchronization using two ways.

  • synchronized method
  • synchronized block

You can not use synchronized with  instance or class variables.

synchronized method

You can make whole incrementCount() method synchronized so no two thread can access it parallelly.

For example:
Thread T1 sees count as 20 and increment it to 21. At the same time, thread t2 will now see count as 21 and increment it to 22.

synchronized block

You can make use block to synchronize critical section in  incrementCount() method so no two thread can access block concurrently.

For example:
Thread T1 sees count as 20 and increment it to 21. At the same time, thread t2 will now see count as 21 and increment it to 22.

15. Can you explain about Object level locking and class level locking?

There are two types of locking in java.

  • Object level locking
  • Class level locking

You can refer Object level locking and class level locking for more details.

16. Can two threads execute static and non static methods concurrently?

Yes, Since two threads will acquire lock on different objects, they can be executed concurrently without any issues.

17. If one method of class is synchronized and other method of same class is not synchronized? Can they be executed concurrently by two threads?

Yes, because one thread will require lock to get into synchronized block but second thread which will execute non synchronized method that won’t require any lock, so it can be executed concurrently.

18. Is it safe to call a synchronized method from another synchronized method?

Yes, it is safe to call a synchronized method from another synchronized method because when you call synchronized method, you will get lock on this object and when you call another synchronized method of same class, it is safe to execute as it already has lock on this object.
For example:

You are actually doing this.

Here if any thread calls method2 from method1, it will already have lock on this object hence It is safe to execute.

19. What is deadlock?

Deadlock is a situation where two or more threads are waiting for each other to release the resource.
For example:
Thread 1 have lock over object 1 and waiting to get lock on object 2. Thread 2 have lock over object 2 and waiting to get lock on object 1. In this scenario, both threads will wait for each other indefinitely.

20. What are differences between notify and notifyall?

You can go through difference between notify and notifyall to see the differences.

You may also like:

Add Comment