daemon 线程 历史由来及详解

https://www.cnblogs.com/xfiver/p/5189732.html

https://laike9m.com/blog/daemon-is-not-daemon-but-what-is-it,97/

守护进程:

Daemon (Daemon Process)" is a notion in UNIX denoting a process detached from any controlling terminal, typically waiting for some event to occur and to respond to in some way. Windows services are similar but I assume Bil et alia chose deliberately a different word for them.

守护进程(Daemon进程)“是UNIX中的一个概念,表示从任何控制终端分离的进程,通常等待某些事件发生并以某种方式响应.Windows服务类似但我认为Bil等人故意选择了另一个单词 对他们来说

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. 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 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.

每个线程都有优先权。 具有较高优先级的线程优先于具有较低优先级的线程执行。 每个线程可能也可能不会被标记为守护进程。 当在某个线程中运行的代码创建一个新的Thread对象时,新线程的优先级最初设置为等于创建线程的优先级,并且当且仅当创建线程是守护进程时才是守护进程线程。

当Java虚拟机启动时,通常会有一个非守护程序线程(通常调用某个指定类的名为main的方法)。 Java虚拟机继续执行线程,直到发生以下任一情况:
*已调用类Runtime的exit方法,并且安全管理器已允许执行退出操作。 *所有非守护程序线程的线程都已死亡,无论是通过调用run方法返回还是抛出一个超出run方法传播的异常。


 

Some threads do background tasks, like sending keepalive packets, or performing periodic garbage collection, or whatever. These are only useful when the main program is running, and it's okay to kill them off once the other, non-daemon, threads have exited.

Without daemon threads, you'd have to keep track of them, and tell them to exit, before your program can completely quit. By setting them as daemon threads, you can let them run and forget about them, and when your program quits, any daemon threads are killed automatically.

有些线程执行后台任务,例如发送keepalive数据包,或执行定期垃圾收集等等。 这些仅在主程序运行时才有用,并且一旦其他非守护程序线程退出就可以将它们终止。

如果没有守护程序线程,您必须跟踪它们,并在程序完全退出之前告诉它们退出。 通过将它们设置为守护程序线程,您可以让它们运行并忘记它们,当程序退出时,任何守护程序线程都会自动终止。

猜你喜欢

转载自blog.csdn.net/u013985241/article/details/86665024