猿问

什么是Java中的守护进程线程?

什么是Java中的守护进程线程?

有人能告诉我Java中有什么守护进程线程吗?



jeck猫
浏览 1122回答 3
3回答

qq_遁去的一_1

守护进程线程是一个线程,它不会阻止JVM在程序完成时退出,但线程仍在运行。守护进程线程的一个示例是垃圾收集。您可以使用setDaemon(boolean)方法更改Thread线程启动之前的守护进程属性。

哔哔one

还有几点(参考:Java并发在实践中的应用)创建新线程时,它继承其父线程的守护进程状态。当所有非守护进程线程完成时,jvm将停止,并且任何剩余的线程都会停止。守护进程线程被放弃:由于这个原因,应该谨慎地使用守护进程线程,使用它们执行任何类型的I/O任务都是危险的。最后,没有执行块。,堆栈没有打开-JVM只是退出。由于这个原因,应该谨慎地使用守护进程线程,使用它们执行任何类型的I/O任务都是危险的。

慕的地10843

以上所有答案都很好。这里有一个简单的代码片段,来说明两者之间的区别。中的true和false的每个值都尝试它。setDaemon.public class DaemonTest {     public static void main(String[] args) {         new WorkerThread().start();         try {             Thread.sleep(7500);         } catch (InterruptedException e) {             // handle here exception         }         System.out.println("Main Thread ending") ;     } } class WorkerThread extends Thread {     public WorkerThread() {         // When false, (i.e. when it's a user thread),         // the Worker thread continues to run.         // When true, (i.e. when it's a daemon thread),         // the Worker thread terminates when the main          // thread terminates.         setDaemon(true);      }     public void run() {         int count = 0;         while (true) {             System.out.println("Hello from Worker "+count++);             try {                 sleep(5000);             } catch (InterruptedException e) {                 // handle exception here             }         }     } }
随时随地看视频慕课网APP

相关分类

Java
我要回答