Last Updated: 09 July, 2023
Java Threads are categorized into two categories:
✅ Daemon Thread
✅ Non-Daemon Thread (User Thread)
A non-daemon thread (user thread) is a front-end thread that executes in the front-end logic of the application, such as writing content, grammar checking, etc., whereas a daemon thread runs in the background of the application and provides service to the non-daemon thread (user thread).
Some important points about the Daemon Thread
Daemon Thread Methods
public final void setDaemon(boolean on)
Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads.
This method must be invoked before the thread is started.
Parameters:
on - if true, marks this thread as a daemon thread
Throws:
IllegalThreadStateException
if this thread is alive
SecurityException
if checkAccess() determines that the current thread cannot modify this thread.
public final boolean isDaemon()
This method is used to check that the current thread is a daemon. It returns true if the thread is Daemon. Else, it returns false.
Returns:
This method returns true if this thread is a daemon thread; false otherwise
Example 1 : Java program to demonstrate the Daemon Thread
Output
Daemon thread is executing
Non-Daemon is thread executing
Please note that we should call the setDaemon() method before calling the start() method if we are making a thread as Daemon. If we call the setDaemon() method after starting the thread, it would throw IllegalThreadStateException.
Let's see the example given below.
Example 2 : IllegalThreadStateException in Daemon Thread
Output
Exception in thread "main" Thread is executing
java.lang.IllegalThreadStateException
at java.base/java.lang.Thread.setDaemon(Thread.java:1410)
at com.multithreading.daemon.thread.example.ExceptionInDaemonThread.main
(ExceptionInDaemonThread.java:17)
Difference between Daemon Thread and Non-Daemon Thread (User Thread)
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
Why does the JVM terminate the daemon thread if there is no user thread?
The main purpose of running the daemon thread is to provide services to the user thread for their background supporting tasks. Once user thread execution is completed, then there is no need to run the daemon thread. Therefore the JVM terminates the daemon thread forcefully.
How can a user thread be converted into a daemon thread and vice versa?
Ans. Java Thread class has provided a method called setDaemon(boolean on), this method takes boolean as a parameter passing true means setting this thread as a daemon thread else false means a non-daemon thread.
This method must be invoked before the thread is started, otherwise it will throw java.lang.IllegalThreadStateException.
class MyUserThread extends Thread {
@Override
public void run() {
System.out.println("Thread is running...");
}
}
public class JavaThreadsInterviewQuestions {
public static void main(String[] args) {
MyUserThread threadObj = new MyUserThread();
threadObj.setDaemon(true);
threadObj.start();
}
}
How we can identify whether the given thread is a daemon or not?
Ans. By using the Thread class isDaemon() method we can easily identify whether the thread is daemon or not. This method returns a boolean as an output: if true, it means this thread is a daemon thread; false otherwise.
threadObj.isDaemon();
What is a green thread?
The thread which is fully managed by the JVM without taking underlying operating system support is known as the green thread. Most operating systems do not support the green thread model. Only a few operating systems, like Sun Solaris, provide support for the green thread model.
Is it possible to make the main() thread a daemon thread?
No, the main() threads are always non-daemon threads in Java, and Java does not allow changing the nature of the non-daemon thread to the daemon thread, so It is not possible.