有人能告诉我Java中的守护线程是什么吗?
当前回答
Daemon thread is like daemon process which is responsible for managing resources,a daemon thread is created by the Java VM to serve the user threads. example updating system for unix,unix is daemon process. child of daemon thread is always daemon thread,so by default daemon is false.you can check thread as daemon or user by using "isDaemon()" method. so daemon thread or daemon process are basically responsible for managing resources. for example when you starting jvm there is garbage collector running that is daemon thread whose priority is 1 that is lowest,which is managing memory. jvm is alive as long as user thread is alive,u can not kill daemon thread.jvm is responsible to kill daemon threads.
其他回答
正如大家所解释的,守护线程不会限制JVM退出,所以基本上从退出的角度来看,它对应用程序来说是一个快乐的线程。
想要添加守护线程,当我提供一个API,比如将数据推送到第三方服务器/或JMS时,我可能需要在客户端JVM级别聚合数据,然后在一个单独的线程中发送到JMS。我可以使这个线程作为守护线程,如果这不是一个强制数据被推送到服务器。 这类数据类似于日志推送/聚合。
问候, Manish
对我来说,守护线程就像用户线程的管家。 如果所有用户线程都已完成,守护进程线程就没有任务 JVM杀死。 我在YouTube视频里解释过了。
当最后一个非守护进程线程执行完成时,JVM将完成这项工作。默认情况下,JVM将创建一个线程作为非守护进程,但我们可以通过setDaemon(true)方法将线程创建为守护进程。Daemon线程的一个很好的例子是GC线程,它将在所有非Daemon线程完成后立即完成它的工作。
Java守护线程
(守护进程)
Java使用用户线程和守护进程线程概念。
JVM流
1. If there are no `user treads` JVM starts terminating the program
2. JVM terminates all `daemon threads` automatically without waiting when they are done
3. JVM is shutdown
正如你所看到的,守护线程是用户线程的服务线程。
守护线程是低优先级线程。 线程从父线程继承它的属性。要从外部设置它,你可以在启动它之前使用setDaemon()方法或通过isDaemon()检查它
Daemon threads are those threads which provide general services for user threads (Example : clean up services - garbage collector) Daemon threads are running all the time until kill by the JVM Daemon Threads are treated differently than User Thread when JVM terminates , finally blocks are not called JVM just exits JVM doesn't terminates unless all the user threads terminate. JVM terminates if all user threads are dies JVM doesn't wait for any daemon thread to finish before existing and finally blocks are not called If all user threads dies JVM kills all the daemon threads before stops When all user threads have terminated, daemon threads can also be terminated and the main program terminates setDaemon() method must be called before the thread's start() method is invoked Once a thread has started executing its daemon status cannot be changed To determine if a thread is a daemon thread, use the accessor method isDaemon()
推荐文章
- 指定的子节点已经有一个父节点。你必须先在子对象的父对象上调用removeView() (Android)
- 对于一个布尔字段,它的getter/setter的命名约定是什么?
- 如何获得当前屏幕方向?
- 如何在Android中渲染PDF文件
- 如何计算一个元素在列表中出现的次数
- c++中类似于java的instanceof
- 我如何解决错误“minCompileSdk(31)指定在一个依赖的AAR元数据”在本机Java或Kotlin?
- 如何POST表单数据与Spring RestTemplate?
- Mockito中检测到未完成的存根
- 我应该如何复制字符串在Java?
- “while(true)”循环有那么糟糕吗?
- 这个方法签名中的省略号(…)是干什么用的?
- Java:如何测试调用System.exit()的方法?
- 带有返回类型的Java方法在没有返回语句的情况下编译
- Java“此语言级别不支持lambda表达式”