Java Multithreading and Concurrency Interview Questions

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

Table of Contents

Java 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
Synchronized

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. Why wait(), notify() And notifyAll() methods are in Object Class?

Answer: 
Thread waits for lock associated with the object and notify other threads which are waiting for same lock.

If wait(), notify() and notifyAll() will be in thread class, then each thread has to be aware of status of another thread and that does not make sense as each thread runs independent of other thread and has no specific knowledge about other thread.

8. Why wait and notify method are called from synchronized block?

Answer: 
wait() is called, so that thread can wait on some condition. When condition is met, then thread has to give up the lock.

To give up the lock, thread has to own it first. Thread can acquire lock by enter into synchronized context.

If wait method is called outside of synchronized context, then it will throw IllegalMonitorStateException.

9. Why sleep() and yield() are static methods in Thread class?

Answer: 
You can call sleep() and yield() method on current executing thread. If there is in wait state, you can not call these methods.

To avoid the confusion for programmers, there methods are made static.

10. 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.

11. 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

12. 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

13. 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.

14. What are daemon threads?

Answer:

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](JVM “JVM”) does not wait for daemon threads to finish.

15. 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.

16. 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.

17. 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 increments 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 increments 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.

18. 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.

19. 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.

20. 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.

21. 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:

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.

22. What is deadlock?

Answer:

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.

23. What are differences between notify and notifyall?

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

24. What is use of volatile keyword in java?

Answer: 
If you make any variable volatile, then this variable will be read from main memory rather then CPU cache, so that each thread will get updated value of the variable.

25. You have started 3 threads and you ought to be certain that main thread finish last. How will you get it done?

Answer: 
You can use thread’s join() method to attain this scenario.

Let’s see with the help of example:
Without join() method:

Output:

Exiting main thread
Current executing thread: Thread2
Current executing thread: Thread1
Current executing thread: Thread3

As you can see, main thread exited first here.
With join() method:

Output:

Current executing thread: Thread3
Current executing thread: Thread2
Current executing thread: Thread1
Exiting main thread

As you can see, main thread exited last after calling join() method on all three threads i.e. thread1, thread2 and thread3.

26.How can you print even odd number using 2 threads?

You have been given 2 threads i.e. T1 and T2. You need to print odd numbers using one thread and even numbers using another thread.
For example:
Let’s say you have to Print even odd number using 2 threads upto 10. Here is the sample expected output.

1 T1
2 T2
3 T1
4 T2
5 T1
6 T2
7 T1
8 T2
9 T1
10 T2

Answer:
Here is the solution using wait and notify in java.
Here is the approach you can use.

  • If num%2==1, then print using T1 and increment it else T1 will go to wait state.
  • If num%2==0, then print using T2 and increment it else T2 will go to wait state.
Output:

T1 1
T2 2
T1 3
T2 4
T1 5
T2 6
T1 7
T2 8
T1 9
T2 10

27. What is ThreadLocal in java?

Answer:
TheadLocal helps you to create variable that can be read and write only by that thread. Two threads can not see each other’s ThreadLocal variable, so even if they are executing same code, there won’t be any race condition and code will be thread safe.
Here is an example:
Create a ThreadLocalRunnable which will have ThreadLocal variable.

Create main class named ThreadLocalMain.

Output:

Thread2:8
Thread1:7

As you can see, Thread1 and Thread2 shares same instance of ThreadLocalRunnable and both set different values for ThreadLocal variable. If we have used synchronized rather than ThreadLocal, later executing thread would have overridden value set by first thread.

28. What is Thread dump?

Answer:
A thread dump is snapshot of all the active threads that are part of process. It contains lot of information about thread and its current state.

A thread dump is very useful when you want to analyze any issue related to deadlock.

You can use jdk tools such as jvisualVM,jstack and Java Mission control. These tools are part of JDK installation and very handy to use.

29. What happens if you don’t override run() method of Thread class?

Answer:
If you don’t override run() method thread class, then nothing will happen.

When you call `run() method of thread class, it calls target.run() method. Here target is instance of Runnable. When you directly create Thread using default constructor, target is set to null.

Let’s see with the help of example:

Output:

Before thread execution
After thread execution

As you can see, nothing happened when we called thread1.start().

Java Concurrency Interview questions

30. What is executor framework?

Answer:
Java 5 has introduced new framework named executor framework for managing threads.

If you need to create thousands of the threads, your application performance may suffer and also, maintenance of each thread is also overhead. Executor framework solves this problem by limiting number of threads and reusing the same threads once threads had completed its current task.

Read more :Executor framework

31. What is Thread pool?

Answer:
Thread pool represents set of worker threads that are waiting for the jobs and these worker threads can be reused many times.

Whenever a task needs to be performance, service provider will pull out a thread out of thread pool to perform the task. If no worker threads are available, then task has to wait for execution.

java.util.concurrent.Executors class provides factory methods to create the thread pools.

32. Can you list down important factory methods of Executors class?

Answer:
Here are the few important methods of Executors class.

newFixedThreadPool: This method returns a thread pool whose maximum size is fixed. If all threads are busy in execution of tasks and additional tasks are submitted, then they have to wait in queue until any thread is available for executing these tasks.

newCachedThreadPool: This method returns unbounded thread pool. If threads are not used for certain defined time (keepAliveTime), then it will kill extra threads.

newSingleThreadedExecutor: This method returns an Executor with single thread.

newScheduledThreadPool: This method returns thread pool with fixed size, that can schedule task periodically or with given delay.

33. Can you write a code to implement newFixedThreadPool?

Answer:
You need to use Executors.newFixedThreadPool(numberOfThreads) to create newFixedThreadPool.

Let’s create very simple example:

Create main class FixedThreadPoolMain.java.

Output:

Starting RunnableTask 1
Starting RunnableTask 2
Starting RunnableTask 3
Executing RunnableTask 2 with pool-1-thread-2====>1
Executing RunnableTask 1 with pool-1-thread-1====>1
Executing RunnableTask 3 with pool-1-thread-3====>1
Executing RunnableTask 1 with pool-1-thread-1====>2
Executing RunnableTask 2 with pool-1-thread-2====>2
Executing RunnableTask 1 with pool-1-thread-1====>3
Executing RunnableTask 3 with pool-1-thread-3====>2
Executing RunnableTask 2 with pool-1-thread-2====>3
Executing RunnableTask 3 with pool-1-thread-3====>3
Finishing RunnableTask 1
Finishing RunnableTask 2
Finishing RunnableTask 3
Starting RunnableTask 4
Executing RunnableTask 4 with pool-1-thread-3====>1
Executing RunnableTask 4 with pool-1-thread-3====>2
Executing RunnableTask 4 with pool-1-thread-3====>3
Finishing RunnableTask 4
Starting RunnableTask 5
Executing RunnableTask 5 with pool-1-thread-3====>1
Executing RunnableTask 5 with pool-1-thread-3====>2
Executing RunnableTask 5 with pool-1-thread-3====>3
Finishing RunnableTask 5

As you can notice in the preceding output, first 3 tasks started executed immediately as we have maximum 3 threads in newFixedThreadPool. Once these 3 tasks are finished, then RunnableTask 4 and RunnableTask 5 got executed.

34. What is BlockingQueue

Answer:
BlockingQueue is special type of queue which is used when producer threads produce object and consumer threads consume object.

Producer thread keeps inserting objects in the queue unless it is full and once it is full, it will be blocked unless consumer threads start consuming.

Similarly, consumer thread keep consuming objects until it is empty. Once it is empty, it will be blocked unless producer threads start producing.

Here are two important methods of BlockingQueue.
put(): This method to put objects in queue until queue is full and waits for consumer thread to take object after that.

take(): This method is used to take objects from the queue until it becomes empty. Once it is empty, it waits for producer threads to start producing the objects and put into the queue.

Let’s see simple example of BlockingQueue.

Output:

Producing element 1
Consumed Element 1
Producing element 2
Consumed Element 2
Producing element 3
Consumed Element 3

35. What are difference between Runnable and Callable interface in java

Refer: Difference between Runnable and Callable in java

36. What is Lock interface? What are its advantages over synchronized block?

java.util.concurrent.lock.Lock is introduced in java 1.5 and it provides important operation for blocking.

It is more flexible and convenient synchronization aid than standard synchronized block.

Advantages of Locks are:

  • Lock supports fairness which can not be achieved in case of synchronized block. We can specify fairness property to make sure longest waiting thread get fair chance for execution.
  • We can use Lock interface’s lock() and unlock() operation in different methods.
  • We can use tryLock() method if we don’t want to block the thread. tryLock() method attempts to Lock immediately and return true if the locking succeeds.
  • We can use lockInterruptibly() method to interrupt the thread which is waiting for the lock.

37. What is Condition interface?

A Condition instance is intrinsically bound to a lock. You can use newCondtion() method of Lock interface to obtain Condition instance.

Condition is useful when you want to create multiple wait-sets per lock.

For example:
In producer consumer problem, if buffer is full, then producer can wait on one Condition instance notEmpty and if buffer is empty, then consumer can wait on another Condition instance notFull unless elements are available.

In case, spaces become available in buffer, consumer can signal producer using Condition instance notEmpty. Similarly, when producer starts adding elements to buffer, it can signal consumer on Condition instance notFull.

38. Implement custom BlockingQueue using Lock and Condition?

Answer:
Here is the implementation of custom BlockingQueue using Lock and Condition.

  • Use array to store elements in BlockingQueue. Size of array will define maximum capacity of the Blocking Queue.
  • Create one lock and two conditions objects: notFull and notEmpty.
  • If Queue is full, then producer has to wait on notFull condition object
  • If Queue is empty, then consumer has to wait on notEmpty condition object
  • Create two threads producer and consumer which will share same CustomBlockingQueue object

Create a class named CustomBlockingQueue.java

Create main class CustomBlockingQueueMain.java.

Output:

Putting in Queue – 1
Putting in Queue – 2
Putting in Queue – 3
Taking from queue – 1
Taking from queue – 2
Taking from queue – 3
Putting in Queue – 4
Putting in Queue – 5
Taking from queue – 4
Taking from queue – 5

As you can see in the output, Producer thread got blocked after putting 3 elements in CustomBlockingQueue as array size was given as 3.

39. What is CountDownLatch?

CountDownLatch is synchronization aid that allows one or more threads to wait until specified operations in other threads complete.

CountDownLatch is initialized by count. Whenever a thread calls latch.awaits() where latch is instance of CountDownLatch, so it will wait until count becomes zero or thread is interrupted by another thread.

So when other threads call latch.countDown(), count is reduced by 1. Once count reaches 0, thread which called latch.await() will be resumed.

CountDownLatch

40. What is CyclicBarrier?

CyclicBarrier is similar to CountDownLatch, but you can reuse once count reaches to 0.

You can also trigger common event once count reaches to 0 in case of CyclicBarrier.

41. What is Semaphore?

When we want to limit number of concurrent threads accessing any resources, we can use Semaphore.

Semaphore maintains set of permits, if permits are not available, thread has to wait.

Semaphores can be used to implement resource pools or bounded collection.

Let’s implement binary semaphore.

Output:

Total available Semaphore permits is: 1
Thread 2 : Getting lock
Thread 1 : Getting lock
Thread 2 : Available Semaphore permits is: 1
Thread 1 : Available Semaphore permits is: 1
Thread 1 : acquired the lock
Thread 1 : is performing task 1, available Semaphore permits : 0
Thread 1 : is performing task 2, available Semaphore permits : 0
Thread 1 : is performing task 3, available Semaphore permits : 0
Thread 1 : is performing task 4, available Semaphore permits : 0
Thread 1 : is performing task 5, available Semaphore permits : 0
Thread 1 : releasing lock…
Thread 2 : acquired the lock
Thread 2 : is performing task 1, available Semaphore permits : 0
Thread 2 : is performing task 2, available Semaphore permits : 0
Thread 2 : is performing task 3, available Semaphore permits : 0
Thread 2 : is performing task 4, available Semaphore permits : 0
Thread 2 : is performing task 5, available Semaphore permits : 0
Thread 2 : releasing lock…
Total available Semaphore permits is: 1

If you notice the output, once the Thread 1 acquired lock, Thread 2 was waiting for permit to be available again. Once Thread 1 released the lock, Thread 2 got the permit and executed the task.

Once Thread 2 released the lock, total available permits again became 1 as expected.

That’s all about multithreading interview questions in java.

You may also like:


You may also like:

Error: View 12d15fexqk may not exist

Was this post helpful?

Leave a Reply

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