java daemon thread and non-daemon thread java daemon thread and non-daemon thread multithreading multithreading

java daemon thread and non-daemon thread


A. When an application begins running, there is one daemon thread, whose job is to execute main().

This is incorrect. See below.

B. When an application begins running, there is one non-daemon thread, whose job is to execute main().

Correct. The JVM exits when the last non-daemon thread exits. If the main thread wasn't non-daemon then the JVM would start up and see that there were no non-daemon threads running and would shutdown immediately.

So therefore the main thread must be a non-daemon thread. For a description of the different between daemon and non, see my answer here: Difference between a daemon thread and a low priority thread

C. A thread created by a daemon thread is initially also a daemon thread.

D. A thread created by a non-daemon thread is initially also a non-daemon thread.

Both are correct. The thread gets its daemon status from the thread that spawned it by default. Daemon threads spawn other daemon threads. Non-daemon threads spawn other non-daemon threads. Looking at the code from Thread.init():

Thread parent = currentThread();...this.daemon = parent.isDaemon();

If you want to change the daemon status then you have to do so before the thread is started.

Thread thread = new Thread(...);// thread has the daemon status of the current thread// so we have to override it if we want to change thatthread.setDaemon(true);// we need to set the daemon status _before_ the thread startsthread.start();


From Thread documentation ,

A thread created by a daemon thread is initially also a daemon thread

Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

When an application begins running, there is one non-daemon thread, whose job is to execute main().

When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:

  • The exit method of class Runtime has been called and the securitymanager has permitted the exit operation to take place.

  • All threads that are not daemon threads have died, either byreturning from the call to the run method or by throwing an exceptionthat propagates beyond the run method.

Daemon and Non-Daemon Thread

A “daemon” thread is one that is supposed to provide a general service in the background as long as the program is running, but is not part of the essence of the program. Thus, when all of the non-daemon threads complete, the program is terminated. Conversely, if there are any non-daemon threads still running, the program doesn’t terminate.

For more explaination refer ThinkingInJava


Daemons threads are those which don't stop the JVM from exiting.Eg. A garbage collection is a daemon thread.

Non-Daemons threads are those like the main thread , on whose exit the JVM also exits i.e the programs also finishes.

By default all threads a non-daemon.