守护程序线程和低优先级线程之间的区别 [英] Difference between a daemon thread and a low priority thread

查看:174
本文介绍了守护程序线程和低优先级线程之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近有人问我一个问题:

Recently I was asked a question:

我们已经有了setPriority()方法来将线程设置为低优先级. 那为什么我们需要一个守护线程.它们之间有什么区别?

We've got the setPriority() method to set a thread for low priority. Then why do we need a daemon thread. What's the difference between them?

将线程标记为守护程序是否会更改其调度?

Does marking a thread as daemon change its scheduling?

推荐答案

我们有setPriority()方法来将线程设置为低优先级.那为什么我们需要一个守护线程.它们之间有什么区别?

We've got the setPriority() method to set a thread for low priority. Then why do we need a daemon thread. What's the difference between them?

通常,守护进程线程与优先级无关.当所有用户非守护程序线程完成时,JVM将关闭.将线程标记为守护程序线程意味着在JVM退出时可以安全地将其杀死.

Typically, daemon threads have nothing to do with priority. The JVM shuts down when all user non-daemon threads finish. Marking a thread as a daemon thread means that it can be safely killed when the JVM exits.

优先级与调度有关–与一个线程相比,该线程与准备运行的其他线程相比,获得时间片的频率.您可以具有低优先级的守护程序线程或高优先级的守护程序线程.您可以具有低优先级和高优先级的非守护线程.顺便说一句,线程优先级仅适用于某些特定情况和某些体系结构,作为Java线程专家,我从不使用它们.

Priority is about scheduling – about how often a thread gets a time slice in comparison to other threads that are ready to run. You can have low priority daemon threads or high priority daemon threads. You can have non-daemon threads that are also low and high priority. As an aside, thread priorities only apply in certain specific situations and on certainly architectures and as a Java thread expert, I never use them.

至少在Java线程模型中,这些概念是正交的(相互独立).

The concepts are orthogonal (mutually independent) – at least in the Java thread model.

就何时创建​​线程守护程序而言,我将守护程序线程用于我不在乎JVM退出时是否中断的任何任务:保持活动线程,统计处理器,日志处理等.所有对应用程序至关重要的任务是非守护线程,必须专门中断该线程或发出以某种方式退出该信号的信号.

In terms of when to make a thread daemon, I use daemon threads for any tasks that I don't care if they are interrupted when the JVM quits: keep-alive threads, statistics processors, log handling, etc.. Everything mission critical to the application is a non-daemon thread that has to be specifically interrupted or signaled to quit somehow.

这篇关于守护程序线程和低优先级线程之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆