java守护线程和非守护线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19505682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java daemon thread and non-daemon thread
提问by Huibin Zhang
I am doing java past exam paper, and I have encounter the following question that is confusing me.
我正在做java过去的试卷,我遇到了以下让我困惑的问题。
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().
A. 当应用程序开始运行时,有一个守护线程,其工作是执行 main()。
B. When an application begins running, there is one non-daemon thread, whose job is to execute main().
B. 当应用程序开始运行时,有一个非守护线程,其工作是执行 main()。
C. A thread created by a daemon thread is initially also a daemon thread.
C. 守护线程创建的线程最初也是守护线程。
D. A thread created by a non-daemon thread is initially also a non-daemon thread.
D. 由非守护线程创建的线程最初也是非守护线程。
The key answer is B,C,D, could anyone tell me why B,C is correct? Many thanks.
关键答案是 B、C、D,谁能告诉我为什么 B、C 是正确的?非常感谢。
采纳答案by Gray
A. When an application begins running, there is one daemon thread, whose job is to execute main().
A. 当应用程序开始运行时,有一个守护线程,其工作是执行 main()。
This is incorrect. See below.
这是不正确的。见下文。
B. When an application begins running, there is one non-daemon thread, whose job is to execute main().
B. 当应用程序开始运行时,有一个非守护线程,其工作是执行 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.
正确的。JVM 在最后一个非守护线程退出时退出。如果主线程不是非守护进程,那么 JVM 将启动并看到没有非守护进程线程在运行并立即关闭。
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.
C. 守护线程创建的线程最初也是守护线程。
D. 由非守护线程创建的线程最初也是非守护线程。
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.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();
回答by Prateek
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.
每个线程可能也可能不标记为守护进程。当在某个线程中运行的代码创建一个新的 Thread 对象时,新线程的优先级最初设置为等于创建线程的优先级,并且当且仅当创建线程是守护进程时,新线程才是守护线程。
When an application begins running, there is one non-daemon thread, whose job is to execute main().
当应用程序开始运行时,有一个非守护线程,其工作是执行 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:
当 a Java Virtual Machine starts up, there is usually a single non-daemon thread
(通常calls the method named main
属于某个指定类)时。Java 虚拟机继续执行线程,直到发生以下任一情况:
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.
已调用 Runtime 类的退出方法并且安全管理器已允许退出操作发生。
所有不是守护线程的线程都已经死亡,要么是从对 run 方法的调用返回,要么是通过抛出传播到 run 方法之外的异常。
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
更多解释请参考ThinkingInJava
回答by Kaveri
Daemonsthreads are those which don't stop the JVM from exiting. Eg. A garbage collection is a daemon thread.
守护进程线程是那些不会阻止 JVM 退出的线程。例如。垃圾收集是一个守护线程。
Non-Daemonsthreads are those like the main thread , on whose exit the JVM also exits i.e the programs also finishes.
非守护进程线程类似于主线程,在其退出时 JVM 也退出,即程序也完成。
By default all threads a non-daemon.
默认情况下所有线程都是非守护进程。