简体   繁体   中英

java daemon thread and non-daemon thread

I am doing java past exam paper, and I have encounter the following question that is confusing me.

Which of the following are true? (Choose all that apply.)

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

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

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.

The key answer is B,C,D, could anyone tell me why B,C is correct? Many thanks.

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 that
thread.setDaemon(true);
// we need to set the daemon status _before_ the thread starts
thread.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 security manager has permitted the exit operation to take place.

  • All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that 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 ie the programs also finishes.

By default all threads a non-daemon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM