有人能告诉我Java中的守护线程是什么吗?
当前回答
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()
守护线程类似于助手线程。非daemon线程就像前面的表演者。助手帮助表演者完成一项工作。工作完成后,执行者不再需要帮助来执行。由于不需要帮助,助手们离开了这个地方。因此,当非守护线程的任务结束时,守护线程就会离开。
守护线程
在后台运行的线程称为守护线程。
Daemon线程示例:
垃圾收集器。 信号分配器。
守护线程的目标:
守护进程线程的主要目标是为非守护进程线程提供支持。
有关守护线程的其他信息:
通常,守护线程在MIN_PRIORITY中运行,但是,也可以使用MAX_PRIORITY运行守护线程。 示例:当需要额外的内存时,GC通常以MIN_PRIORITY优先级运行。JVM将GC的优先级从MIN_PRIORITY增加到MAX_PRIORITY。
一旦线程已经启动,就不能将其从守护线程更改为非守护线程,这会导致IllegalThreadStateException异常。 例子: public static void main(String[] args) { Thread.currentThread () .setDaemon(真正的); } 输出: java.lang.IllegalThreadStateException线程异常 在java.base / java.lang.Thread.setDaemon (Thread.java: 1403)
If we are branching off a thread, the child thread inherits the nature of the parent thread. If the parent thread is a non-daemon thread automatically the child thread will be non-daemon as well and if the parent thread is a daemon, the child thread will be a daemon as well. class Scratch { public static void main(String[] args) { CustomThread customThread = new CustomThread(); customThread.start(); } } class CustomThread extends Thread{ @Override public void run() { System.out.println(currentThread().isDaemon()); } } Output: false
When the last non-daemon thread terminates, all the daemon threads get terminated automatically. class Scratch { public static void main(String[] args) { System.out.println("Main Thread Started."); CustomThread customThread = new CustomThread(); customThread.setDaemon(true); customThread.start(); System.out.println("Main Thread Finished."); } } class CustomThread extends Thread{ @Override public void run() { System.out.println("Custom Thread Started."); try { sleep(2000); } catch (InterruptedException ignore) {} System.out.println("Custom Thread Finished."); //Won't get executed. } } Output: Main Thread Started. Main Thread Finished. Custom Thread Started.
守护线程就像一个普通的线程,除了JVM只会在其他非守护线程不存在时关闭。守护进程线程通常用于为应用程序执行服务。
当最后一个非守护进程线程执行完成时,JVM将完成这项工作。默认情况下,JVM将创建一个线程作为非守护进程,但我们可以通过setDaemon(true)方法将线程创建为守护进程。Daemon线程的一个很好的例子是GC线程,它将在所有非Daemon线程完成后立即完成它的工作。
推荐文章
- Java泛型什么时候需要<?扩展T>而不是<T>,切换有什么缺点吗?
- 如果性能很重要,我应该使用Java的String.format()吗?
- getResourceAsStream返回null
- 如何使用Java中的Scanner类从控制台读取输入?
- 如何添加JTable在JPanel与空布局?
- Statement和PreparedStatement的区别
- 为什么不能在Java中扩展注释?
- 在Java中使用UUID的最重要位的碰撞可能性
- 转换列表的最佳方法:map还是foreach?
- 如何分割逗号分隔的字符串?
- Java字符串—查看字符串是否只包含数字而不包含字母
- Mockito.any()传递带有泛型的接口
- 在IntelliJ 10.5中运行测试时,出现“NoSuchMethodError: org.hamcrest. matcher . descripbemismatch”
- 使用String.split()和多个分隔符
- Java数组有最大大小吗?