java - 可以将 main() 转换为守护线程

标签 java multithreading daemon program-entry-point

据我所知,Java中的main()默认是一个非守护线程,是否可以将其转换为守护线程?

最佳答案

如果只有守护线程在运行,那么 JVM 将关闭。如果主线程是守护线程,那么程序必须立即关闭才能运行。此外,您不能在线程启动后在其上设置守护进程属性,您不能在运行时将非守护线程更改为守护线程:

public final void setDaemon(boolean on)

Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads.

This method must be invoked before the thread is started.

关于java - 可以将 main() 转换为守护线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32726757/

相关文章:

java - 如何在 Spring 中处理 ThreadPool/ExecutorService

python - Systemd 服务不执行我的 Python 脚本

java - 测试JavaMail相关模块

java - 意外绑定(bind)

java - MySQL中逐行添加和多行添加之间最快的是什么

java - 经过一定时间后,使线程最终进入

c++ - 引导序列中的多线程应用程序 - C++/Debian

objective-c - 越狱ios中的USB检测

python - celeryd 和 celerybeat - "Received unregistered task of type..."

java - org.apache.wicket.util.resource.IResourceStream 抛出 javascript 错误。不能下载文件