daemon and non-daemon threads, setDaemon method and Java Thread Concepts
daemon Thread
Java has a special kind of thread called daemon thread.
These kind of threads have very low priority and normally only execute when no other thread of the same program is running.
When daemon threads are the only threads running in a program, the JVM ends the program finishing these threads.
With these characteristics, the daemon threads are normally used as service providers for normal (also known as user) threads running in the same program. They usually have an infinite loop that waits for the service request or performs the tasks of the thread. They cannot do important jobs because we do not know when they are going to have CPU time and they can finish any time if there are no other threads running. A typical example of these kind of threads is the Java garbage collector.
non-daemon Thread
A Java program ends when all its threads finish (more specifically, when all its non-daemon threads finish). If the initial thread (the one that executes the main() method) ends, the rest of the threads will continue with their execution until they finish.
If one of the threads use the System.exit()
instruction to end the execution of the program, all the threads end their execution.Creating an object of the Thread class doesn't create a new execution thread.
Also, calling the run()
method of a class that implements the Runnable interface does not create a new execution thread. Only calling the start()
method creates a new execution thread.
Java Concurrency
setDaemon Threads
The
JVM will be alive until all the "non-deamon threads" have finished execution. When you set the daemon flag to false, the thread is a "non-deamon" and will keep running.
However, if you set the daemon flag to true,
t.setDaemon(true);
as soon as the
main()
method completes execution, the JVM will exit.
The JVM will not shut down until all the "non-daemon threads" complete.
Question: Can we force all threads to exit before the main exit by using setDaemon(true);
?
Answer: Yes, unless System.exit()
is called.
There is a thread called the "main thread", which is the one that runs the main()
method. This thread is just another "non-daemon thread" with the name main and it starts before the other "non-daemon threads".
However, the JVM will not terminate until all "non-daemon threads" have completed, or until System.exit()
is called.
When there are no more "non-daemon threads", there may or may not be "daemon threads" present.
In any case, the JVM can exit, and any remaining "daemon threads" are killed when the JVM exits.
Nevertheless, you cannot force the main()
method to exit 1) before or 2) after the "daemon threads" terminate.
When the JVM exits, it automatically terminates any remaining threads as it exits.
The "main thread" can exit even if the daemon threads are running since the main daemon is just another thread that runs the "main() method" in Java. The JVM will not shut down if there are still "non-daemon threads" running.
Question: In a normal case (when there are no "non-daemon threads" running), does the JVM exit with the main thread?
Answer: The JVM will exit shortly after the last "non-daemon thread" exits.
The last "non-daemon thread" may be the main thread (for the main()
method), or it may be a different thread.
As part of the JVM shutdown, any remaining "daemon threads" will also be shut down.
This is not something you can force, it is something that will always happen when the JVM shuts down, if there are any "daemon threads" remaining.
What will the following program print when run?
class MyThread extends Thread{
public void run(){
for(;;){
System.out.println("running...");
}
}
}
public class TestClass{
public static void main(String args[]) throws Exception{
MyThread mt = new MyThread();
mt.setDaemon(true);
mt.start();
mt.setDaemon(false);
}
}
Select 1 option:
- It will keep printing "running..." for ever.
- It will throw an exception at runtime.
- It will print "running..." for some time and end without any exception.
- It will not even compile.
Answer: b
Explanation:
You cannot call
setDaemon()
after the thread is started. It will throw
IllegalThreadStateException if you do so. Remember that same exception is thrown if you try to call start() on a thread that is already started.