有人能告诉我 Java 中的守护线程是什么吗?
守护线程是一种不会阻止JVM在程序结束后退出但线程仍在运行的线程。垃圾回收就是一个守护线程的例子。
您可以使用setDaemon(boolean)
方法在线程启动之前更改Thread
守护属性。
setDaemon(boolean)
只能在线程启动之前调用。默认情况下,线程会继承其父线程的后台状态。 - Gray以下是一些要点(参考:Java并发编程实战)
当所有非守护线程完成后,JVM停止运行,任何剩余的守护线程都会被弃用:
由于这个原因,应该谨慎使用守护线程,并且千万不能将它们用于可能执行任何类型的I/O的任务。
finally
块,无论这些线程是否为守护线程。因此,如果您认为可能有正在运行I/O操作的线程,请不要调用System.exit(…)
。唯一的区别是当只剩下守护线程时,JVM将触发自己的终止。 - Holger所有上述答案都很好。这里是一个简单的代码片段,用于说明区别。尝试在setDaemon
中使用true和false的每个值。
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 non daemon thread),
// the WorkerThread continues to run.
// When true, (i.e. when it's a daemon thread),
// the WorkerThread terminates when the main
// thread or/and user defined thread(non daemon) 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
}
}
}
}
catch (InterruptException)
块中也有类似于println()的东西,这将清楚地表明守护线程不是通过中断机制退出的,而是突然停止存在。 - antak在UNIX中,传统上Daemon进程是那些像Windows服务一样一直在后台运行的进程。
Java中的Daemon线程是指不会阻止JVM退出的线程。具体来说,只有当Daemon线程存在时,JVM才会退出。您可以通过在Thread
上调用setDaemon()
方法来创建一个Daemon线程。
请阅读有关Daemon线程的内容。
守护线程像服务提供者,为运行在同一进程中的其他线程或对象提供支持。守护线程用于后台支持任务,只有在正常线程执行时才需要。如果没有正常线程正在运行且剩余线程都是守护线程,则解释器将退出。
例如,HotJava 浏览器使用最多四个名为“Image Fetcher”的守护线程,为需要图像的任何线程从文件系统或网络获取图像。
守护线程通常用于为应用程序/小程序执行服务(如加载“琐碎的部分”)。用户线程和守护线程之间的核心区别在于,当所有用户线程终止时,JVM 才会关闭程序。当不再运行任何用户线程(包括主执行线程)时,JVM 将终止守护线程。
setDaemon(true/false)? 此方法用于指定线程是否为守护线程。
public boolean isDaemon()? 此方法用于确定线程是否为守护线程。
例如:
public class DaemonThread extends Thread {
public void run() {
System.out.println("Entering run method");
try {
System.out.println("In run Method: currentThread() is" + Thread.currentThread());
while (true) {
try {
Thread.sleep(500);
} catch (InterruptedException x) {}
System.out.println("In run method: woke up again");
}
} finally {
System.out.println("Leaving run Method");
}
}
public static void main(String[] args) {
System.out.println("Entering main Method");
DaemonThread t = new DaemonThread();
t.setDaemon(true);
t.start();
try {
Thread.sleep(3000);
} catch (InterruptedException x) {}
System.out.println("Leaving main method");
}
}
输出:
C:\java\thread>javac DaemonThread.java
C:\java\thread>java DaemonThread
Entering main Method
Entering run method
In run Method: currentThread() isThread[Thread-0,5,main]
In run method: woke up again
In run method: woke up again
In run method: woke up again
In run method: woke up again
In run method: woke up again
In run method: woke up again
Leaving main method
C:\j2se6\thread>
守护进程:来自d(isk) a(nd) e(xecution) mon(itor) 或者de(vice) mon(itor)
一种后台进程,处理像打印作业和文件传输等服务请求,并在不需要时处于休眠状态。
—— 来源:牛津字典英语
守护线程是一种被认为在后台执行某些任务的线程,例如处理请求或应用程序中可能存在的各种定时任务。
当程序中只有 守护线程 时,程序将退出。这是因为通常这些线程与普通线程一起工作,并提供事件的后台处理。
您可以使用 setDaemon
方法指定一个Thread
是守护线程,它们通常不会退出,也不会被中断...当应用程序停止时它们才会停止。
我想要澄清的一个误解:
守护线程和用户线程。通常情况下,程序员创建的所有线程都是用户线程(除非您指定其为守护线程或者您的父线程是守护线程)。 用户线程通常用于运行我们的程序代码。 JVM直到所有用户线程终止后才会终止。
Java拥有一种特殊的线程,称为守护线程。
守护线程通常用作正常线程的服务提供者。它们通常具有无限循环,等待服务请求或执行线程的任务。它们不能做重要的工作。(因为我们不知道它们何时会有CPU时间,并且如果没有其他线程运行,它们随时可能完成。)
这种线程的典型示例是Java垃圾收集器。
还有更多...
start()
方法之前,才能调用setDaemon()
方法设置线程为守护线程。一旦线程开始运行,就不能修改其守护进程状态。isDaemon()
方法来检查线程是否为守护线程或用户线程。
Thread
javadoc描述了它们是什么:http://java.sun.com/javase/6/docs/api/java/lang/Thread.html - skaffman