➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle Database Size
➤ Check Oracle Database Version
➤ Generation of Computers
➤ XML Advantages & Disadvantages
➤ Advantages of Open Source Software
➤ How to read and display .class file version
➤ Java Program Without Using User-defined Class
Java Multithreading Interview Questions Part-5 | In the previous Java Multithreading Interview Questions Part – 4 we have seen the Java Synchronization Interview Question. Now, in this post, we will discuss the Inter-Thread Communication, Deadlock, Daemon Thread Interview Questions.
- Top Java Multithreading Interview Questions
- Part-1:- Introduction to Java Multithreading Questions
- Part-2:- Defining a thread, run(), start(), setName(), getName()
- Part-3:- Thread Priority, yield(), join(), sleep() & interrupt()
- Part-4:- Synchronization in Java
- Part-5:- Inter-thread Communication, Deadlock, Daemon Thread
Q1) What is inter-thread communication?
The process of executing multiple threads alternatively in sequence with communication for modifying and reading data from the same object is called inter-thread communication. We develop this concept when two different dependent tasks need to be executed continuously in sequence by two different threads on the same object.
Q2) How can two threads communicate with each other?***
Two threads can communicate with each other by using wait(), notify(), and notifyAll() methods.
Q3) The wait(), notify(), and notifyAll() methods are given in which class?
These methods are given in java.lang.Object class.
Q4) Why wait(), notify(), and notifyAll() methods are defined in java.lang.Object class but not in Thread class or Runnable interface?***
Thread can call wait(), notify(), and notifyAll() 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 superclass of all Java classes therefore they are defined in the java.lang.Object class.
Q5) The wait(), notify(), and notifyAll() methods must be called from?
We can call these methods only from the synchronized area (block/method) else IllegalMonitorStateException will be raised at runtime.
Q6) What happens when a thread calls the wait() method on any object?
When a thread calls the wait() method on an object then immediately thread releases the lock of that particular object and enters into the waiting state.
Note that only the lock of that particular object is released, it won’t release the lock of other objects. Suppose at the time of calling wait() method, thread contains the lock of 10 objects then the thread will release lock for only 1 object on which wait() method was called, it won’t release lock for the remaining 9 objects.
Q7) What happens when a thread calls notify() method on any object?
When a thread calls notify() method on any object then it releases the lock of the object but may not immediately. And it also gives notification to the waiting thread.
For example, the x.wait() method was called by one thread. This thread will release the lock and went into the waiting state to get the notification.
After calculation, x.notify() was called by another thread. Therefore this thread will release the lock of the object and give notification to the waiting thread.
Q8) What will happen if we do not call notify() or notifyAll() methods on the waiting thread?
That thread will be in the waiting state forever.
Q9) What are the methods due to them a thread can release the lock?
The wait(), notify(), and notifyAll() are only methods where the thread releases the lock.
Q10) 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.
Q11) What is the exception we must handle while calling the wait() method?
Q12) What is the difference between sleep(100), join(100), and wait(100) method calls?
- The sleep(100) blocks thread execution independent of other threads for 100 milliseconds.
- The join(100) blocks thread execution by depending on either other thread execution is completed or till 100 milliseconds completed, whichever coming first thread resumes its execution immediately.
- The wait(100) blocks thread execution by depending on either other thread till it is called notify() or till 100 milliseconds completed, whichever coming first thread resumes its execution immediately.
Q13) What are the rules in using wait(), notify(), and notifyAll() methods?
- The wait() method throws an InterruptedException, which is a checked exception. Therefore it must be handled whenever wait() is called.
- The wait(), notify(), and notifyAll() method must be called only in synchronized method else we will get an IllegalMonitorStateException.
- If we call the no-arg wait() method, later from another thread then we must call notify()/notifyAll() method else this thread execution is in WAITING state forever.
- If we want to resume thread after some time even though notify() method is not called we must use wait(long time) or wait(long ms, int ns) method.
Deadlock and Starvation
Q1) What is a 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. See more:- Deadlock in Java with example.
Q2) What is the keyword that 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.
Q3) How to avoid a deadlock situation?
We can avoid deadlock situation by using follows:-
- Avoid nested locks:- Avoid nested locks as much as possible.
- Avoid unnecessary locks:- Use locks only on necessary objects.
- Use thread join:- Use join(long ms) or join(long ms, int ns) to wait for a certain period of time.
- Use java.util.concurrent package.
Q4) Write a Java program to demonstrate deadlock in Java?
We discussed this program in detail here:- Deadlock in Java
Q5) What is the difference between deadlock and starvation?
Long time waiting of two threads for each other where waiting never ends is called deadlock whereas the long time waiting of a thread where waiting may end at a certain point is called starvation. See more:- Starvation in Java
Q7) What is livelock?
A thread often acts in response to the action of another thread. If the other thread’s action is also a response to the action of another thread, then livelock may result. As with deadlock, livelocked threads are unable to make further progress.
Q1) How many types of threads can be created in JVM?
We can create two types of threads:- non-daemon thread and daemon thread.
Q2) What is a daemon thread in Java?
A thread that is running in the background based on a schedule to provide services to non-daemon threads is called a 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. See more:- Daemon Thread in Java
Q3) What is a non-daemon 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.
Q4) Why are most of the threads created as non-daemon threads?
The daemon/non-daemon nature of thread is inherited from the parent thread. If the parent thread is a daemon thread then the child thread also will be a daemon thread and similarly, if the parent thread is a non-daemon thread then the child thread will be a non-daemon thread.
Most of the threads are created by the main thread. Since the main thread is a non-daemon thread, therefore, all the child threads created by the main method are also non-daemon threads.
Q5) How can we find whether the given thread is a daemon or not?
We can check the daemon nature of the thread by using the isDaemon() method of the Thread class.
Q6) Which method can be used to create a daemon thread?
We can change the daemon nature of the thread by using the setDaemon() method of the Thread class.
Q7) What is the rule in calling the setDaemon() method?
The setDaemon() method must be called before starting of the thread else we will get java.lang.IllegalThreadStateException.
Q8) Is it possible to change the daemon property after starting a thread?
No, because the setDaemon() method must be called before starting the thread.
Q9) Is it possible to change the daemon nature of the main thread?
No, there is no way to change the daemon nature of the main thread because the main thread is already started by the JVM at the very beginning. Therefore it is impossible to change the daemon nature of the main thread.
Q10) When is the daemon thread execution completed?
The main purpose of the daemon thread is to provide services to the non-daemon threads. If there is no non-daemon thread left in the program then daemon threads will be terminated.
Q11) If we create a custom thread as a daemon can we guarantee its full execution?
No, once the last non-daemon thread in the program terminates then all daemon threads of the program will be terminated immediately. Hence, full execution of the daemon thread is not guaranteed.
Q12) JVM waits for which thread to complete:- non-daemon or daemon?
JVM waits only for non-daemon threads to complete.
Q13) What is a 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.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!