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


当前回答

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

例如,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>

其他回答

守护线程就像一个普通的线程,除了JVM只会在其他非守护线程不存在时关闭。守护进程线程通常用于为应用程序执行服务。

守护线程是在后台执行一些任务的线程,比如处理请求或应用程序中可能存在的各种chronjob。

当你的程序只剩下守护线程时,它将退出。这是因为这些线程通常与普通线程一起工作,并提供事件的后台处理。

你可以使用setDaemon方法指定一个线程是守护线程,它们通常不会退出,也不会被中断。当应用程序停止时,它们就会停止。

守护进程线程通常被称为“服务提供者”线程。这些线程不应该用来执行程序代码,而应该用来执行系统代码。这些线程与您的代码并行运行,但JVM可以随时终止它们。当JVM没有发现用户线程时,它将停止用户线程,所有守护进程线程立即终止。我们可以使用以下方法将非守护线程设置为守护线程:

setDaemon(true)

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()

对我来说,守护线程就像用户线程的管家。 如果所有用户线程都已完成,守护进程线程就没有任务 JVM杀死。 我在YouTube视频里解释过了。