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


当前回答

守护进程: d(isk) a(nd) e(xecution) mon(itor) or or from de(vice) mon(itor)

Daemon(计算)的定义:

处理打印假脱机和文件传输等服务请求的后台进程,在不需要时处于休眠状态。

——来源:牛津词典英文版

Java中的守护线程是什么?

Daemon threads can shut down any time in between their flow, Non-Daemon i.e. user thread executes completely. Daemon threads are threads that run intermittently in the background as long as other non-daemon threads are running. When all of the non-daemon threads complete, daemon threads terminates automatically. Daemon threads are service providers for user threads running in the same process. The JVM does not care about daemon threads to complete when in Running state, not even finally block also let execute. JVM do give preference to non-daemon threads that is created by us. Daemon threads acts as services in Windows. The JVM stops the daemon threads when all user threads (in contrast to the daemon threads) are terminated. Hence daemon threads can be used to implement, for example, a monitoring functionality as the thread is stopped by the JVM as soon as all user threads have stopped.

其他回答

我想澄清一个误解:

Assume that if daemon thread (say B) is created within user thread (say A); then ending of this user thread/parent thread (A) will not end the daemon thread/child thread (B) it has created; provided user thread is the only one currently running. So there is no parent-child relationship on thread ending. All daemon threads (irrespective of where it is created) will end once there is no single live user thread and that causes JVM to terminate. Even this is true for both (parent/child) are daemon threads. If a child thread created from a daemon thread then that is also a daemon thread. This won't need any explicit daemon thread flag setting. Similarly if a child thread created from a user thread then that is also a user thread, if you want to change it, then explicit daemon flag setting is needed before start of that child thread.

守护线程类似于助手线程。非daemon线程就像前面的表演者。助手帮助表演者完成一项工作。工作完成后,执行者不再需要帮助来执行。由于不需要帮助,助手们离开了这个地方。因此,当非守护线程的任务结束时,守护线程就会离开。

再讲一点(参考:Java并发实践)

当创建一个新线程时,它将继承其守护进程状态 的父母。 当所有非守护进程线程完成时,JVM停止,并放弃所有剩余的守护进程线程: 最后,块不执行, 栈不会被解开——JVM只是退出。 由于这个原因,应该谨慎使用守护线程,将它们用于可能执行任何类型的I/O的任务是危险的。

守护线程就像一个服务提供者,为运行在与守护线程相同进程中的其他线程或对象提供服务。守护线程用于后台支持任务,只有在正常线程执行时才需要守护线程。如果正常线程不运行,其余线程是守护线程,则解释器退出。

例如,HotJava浏览器使用最多4个名为“Image Fetcher”的守护线程从文件系统或网络中为任何需要的线程获取图像。

守护线程通常用于为应用程序/applet执行服务(例如加载“fiddley bits”)。用户线程和守护线程之间的核心区别在于,JVM只会在所有用户线程都终止时关闭程序。当不再有任何用户线程在运行(包括执行主线程)时,守护线程将由JVM终止。

setDaemon(真/假)?此方法用于指定一个线程是守护线程。

公共boolean isDaemon() ?此方法用于确定线程是否是守护线程。

Eg:

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>

以上答案都是好的。下面是一个简单的小代码片段,以说明两者的区别。在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
            }
        }
    }
}