Java 如何创建守护线程?为了什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19421027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:00:31  来源:igfitidea点击:

How to create a daemon thread? and what for?

javamultithreading

提问by

I cannot understand the usage and the purpose of daemon threads.

我无法理解守护线程的用法和目的。

What are they for? How can I use them? Also, I tried to create daemons but I couldn't.

它们是为了什么?我该如何使用它们?此外,我尝试创建守护进程,但我不能。

class Evil implements Runnable {
    public static void main(String[] arg) throws Exception {
        Thread t = new Thread(new Evil());
        t.start();
        Thread.sleep(1000);
        t.setDaemon(true);//no success, error!
    }

    public void run() {
        try {
            Thread.sleep(1000);
            System.out.println("How would it be Evil!?");
            Thread.sleep(1000);
        } catch (Exception e) {
        }
    }
}

This is what I attempted so far, but it's not working properly.

这是我到目前为止所尝试的,但它不能正常工作。

采纳答案by Kayaman

First you need to set a thread as daemon just before you start it, so the first thing would be like this:

首先,您需要在启动之前将线程设置为守护进程,所以第一件事是这样的:

 Thread t = new Thread(new Evil());
 t.setDaemon(true);//success is here now
 t.start();
 Thread.sleep(1000);

Daemon threads are like normal (user) threads, but there is a big difference. The JVM kills (halt) the application when there is no user thread exist (alive), in other word if you have 1 user thread (main thread for example) and 1000 daemon threads, here the JVM sees one thread in your application, and it kills the application just after that main thread finishes its job.

守护线程就像普通(用户)线程,但有很大的不同。当没有用户线程存在(活动)时,JVM 会终止(停止)应用程序,换句话说,如果您有 1 个用户线程(例如主线程)和 1000 个守护线程,那么 JVM 会在您的应用程序中看到一个线程,并且它在主线程完成其工作后立即终止应用程序。

These threads are good for handling or doing some business logic in the background till other user threads alive, and beware about changing anything withing daemon thread, because there is no any signal before halting a thread by JVM.

这些线程非常适合在后台处理或执行一些业务逻辑,直到其他用户线程处于活动状态,并注意守护线程中的任何更改,因为在 JVM 停止线程之前没有任何信号。

So in you case, where daemon thread waits for 1 second and say something and again sleep for 1 second, because this is daemon, and main threads is no more after 1 second, then daemon thread never reaches the second sleep line.

因此,在您的情况下,守护线程等待 1 秒并说些什么并再次休眠 1 秒,因为这是守护进程,并且主线程在 1 秒后不再存在,那么守护线程永远不会到达第二个睡眠线。

This (diagram)may help you too. from arashmd.blogspot.com

这个(图表)也可以帮助你。 来自 arashmd.blogspot.com

回答by René Link

The javadoc for Thread.setDaemon(boolean)says:

javadoc forThread.setDaemon(boolean)说:

Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads.

This method must be called before the thread is started.

将此线程标记为守护线程或用户线程。当唯一运行的线程都是守护线程时,Java 虚拟机退出。

必须在线程启动之前调用此方法。

A good example for a deamon thread is a timer.

守护线程的一个很好的例子是计时器。

It makes no sense that a timer fires one more time if there are no user threads anymore.

如果不再有用户线程,计时器再触发一次是没有意义的。

回答by Kayaman

  1. Daemon threads aren't evil (although technically they could do evil things).
  2. You can't make a thread daemon after it has been started.
  3. You would use a daemon thread as a background thread that mustn't/doesn't need to prevent the program from closing.
  1. 守护线程并不是邪恶的(尽管从技术上讲它们可以做邪恶的事情)。
  2. 线程守护进程启动后就不能再创建了。
  3. 您将使用守护线程作为后台线程,它不能/不需要阻止程序关闭。

回答by Narendra Pathai

Daemon status must be set before starting the thread

必须在启动线程之前设置守护进程状态

A daemon thread is a thread, that does not prevent the JVM from exiting when the program finishes but the thread is still running. An example for a daemon thread is the garbage collection.

守护线程是一个线程,当程序完成但线程仍在运行时,它不会阻止 JVM 退出。守护线程的一个例子是垃圾收集。