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?

Answer:
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?

Answer:]
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?

Answer:
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?

Answer:
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?

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

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

Parameter
wait
sleep
Synchonized
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()
static
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?

Answer: 
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?

Answer: 
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?

Answer: 
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?

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

11. What are daemon threads?

Answer: Daemon threads are low priority background threads which provide services to user threads. It's life depends on user threads. If no user thread is running then JVM can exit even if daemon threads are running. JVM do 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?

Answer: 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 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:
package org.arpit.java2blog;

public class RequestCounter {
 
 private int count;
 
 public int incrementCount()
 {
  count++;
  return count;
 }
}
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.
package org.arpit.java2blog;

public class RequestCounter {
 
 private int count;
 
 public synchronized int incrementCount()
 {
  count++;
  return count;
 }
}
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.
package org.arpit.java2blog;

public class RequestCounter {

 private int count;

 public int incrementCount() {
  synchronized (this) {
   count++;
   return count;
  }
 }
}
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
Object level locking:
Object level locking means you want to synchronize non static method or block so that it can be accessed by only one thread at a time for that instance. It is used if you want to protect non static data.
You can achieve Object level locking by following.

Make method synchronized:

public synchronized int incrementCount()
{
}

Using synchronized block and lock on this:

public int incrementCount() {
  synchronized (this) {
   count++;
   return count;
  }

Using synchronized block and lock on some other object:

private final Object lock=new Object();
public int incrementCount() {
  synchronized (lock) {
   count++;
   return count;
  }
Class level locking:
Class level locking means you want to synchronize static method or block so that it can be accessed by only one thread for whole class. If you have 10 instances of class, only one thread will be able to access only one method or block of any one instance at a time. It is used if you want to protect static data.
This can be achieved by following:

Make static method synchronized:

public static synchronized int incrementCount()
{
}

Using synchronized block and lock on .class:

public int incrementCount() {
  synchronized (RequestCounter.class) {
   count++;
   return count;
  }

Using synchronized block and lock on some other static object:

private final static Object lock=new Object();
public int incrementCount() {
  synchronized (lock) {
   count++;
   return count;
  }

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

Answer:
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?

Answer:
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?

Answer:
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:
public synchronized void method1() {
  method2();
  // some code
 }
 
 public synchronized void method2() {
  // some code
 }
You are actually doing this.
public void method1() {
  synchronized (this) {
   method2();
   // some code
  }
  
 }
 
 public void method2() {
  synchronized (this) {
  // some code
  }
 }
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?

Answer:
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?

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

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012