Earlier I have shared 100+ core java interview questions based on various topics of core java. In this article I am gonna share interview questions based on multithreading and concurrency only. You would face multithreading questions in almost all the interviews as this is one the frequently asked topic during interviews for java professionals.
If you are new to this topic, I would recommend you to refer this tutorial (java multithreading) before going through the below set of interview questions and answers.
Interview Questions on Multithreading
Q) What is thread in Java?
A) A Thread is a concurrent unit of execution. Or in other words you can say that it is part of process that can run concurrently with other parts of the process.
Q) What is Multithreading?
A) The process of executing multiple threads simultaneously is known as multithreading. Java supports multithreading. The main advantage of multithreading is reducing CPU idle time and improving the CPU utilization. This makes the job to be completed in less time.
Q) What is the difference between thread and Process?
A) Refer this article: Thread vs Process
Q) What are the two ways of creating a thread?
A) We can create a thread by using any of the two following methods.
1) By implementing Runnable interface
2) By extending Thread class
Q) What is a Daemon thread?
A) Refer this article: Deamon thread in Java
Q) How to make a thread (user thread) to Daemon thread?
A) By calling setDaemon() method we can make a user thread to daemon thread.
Q) What is difference between user thread and Daemon thread?
A) By default a thread created in a program is always a user thread, however we can make it daemon by calling setDaemon(true) method, if needed. A daemon thread runs in a background and it doesn’t prevent JVM from being shutdown. Once all the user thread gets completed the JVM shutdowns without being bothered whether a daemon thread is running or not.
Q) Can we change a user thread to deamon thread by calling setDaemon() method if the thread has already been started?
No, if the thread has been started then we cannot make it daemon because it would then throw an IllegalThreadStateException
Q) Can we call run() method of Thread class?
A) We can call run() method if we want but then it would behave just like a normal method and we would not be able to take the advantage of multithreading. In general run() methods starts execution when we call start() method of a Thread class. For more details on this: Refer this article.
Q) What is deadlock?
A) A deadlock is a condition when two or more threads are in waiting for each other to release the resources that they need. For example Thread A holds a resource X and need resource Y whereas Thread B holds a resource Y and need X, in this case both threads are waiting for each other to release the resource and are in blocked condition.
Q) What is synchronization?
A) It is a technique of granting access to the shared resources in multithread environment to avoid inconsistencies in the results.
Q) What is the difference between notify() and notifyAll()?
A) notify() wakes up the first thread that called wait() on the same object, whereas the notifyAll() method wakes up all the waiting threads.
Q) What does join() method do?
A) The join() method is used to hold the execution of currently running thread until the specified thread is dead(finished execution).
Read more about join() here.
Q) Can we start a thread twice in Java?
No, once a thread is started, it can never be started again. Doing so will throw an illegalThreadStateException. For example: Refer this article.
plz explain runnable interface in thread with example