什么是“长时间运行的任务"?意思是? [英] What does "long-running tasks" mean?

查看:96
本文介绍了什么是“长时间运行的任务"?意思是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,CLR在池线程上运行任务,这非常适合 短期运行的计算绑定工作.用于长时间运行和阻塞 操作中,可以防止使用池线程,如下所示:

By default, the CLR runs tasks on pooled threads, which is ideal for short-running compute-bound work. For longer-running and blocking operations, you can prevent use of a pooled thread as follows:

Task task = Task.Factory.StartNew (() => ...,
TaskCreationOptions.LongRunning);

我正在阅读有关threadtask的主题.您可以向我解释什么是长期运行"和短期运行"的任务吗?

I am reading topic about thread and task. Can you explain to me what are "long[er]-running" and "short-running" tasks?

推荐答案

通用线程池中,您可以根据启动时间和启动时间之间的比较来区分短期线程和长期线程.运行时间.

In general thread pooling, you distinguish short-running and long-running threads based on the comparison between their start-up time and run time.

创建线程通常花费一些时间,直到达到可以开始运行代码的地步.

Threads generally take some time to be created and get up to the point where they can start running your code.

这意味着,如果您运行大量线程,每个线程需要一分钟才能启动,而仅运行一秒钟(时间不准确,但这里的目的只是为了显示关系),则每个线程的运行时间将首先让他们陷入困境的时间被淹没了.

The means that if you run a large number of threads where they each take a minute to start but only run for a second (not accurate times but the intent here is simply to show the relationship), the run time of each will be swamped by the time taken to get them going in the first place.

这是使用线程池的原因之一:线程一旦完成工作就不会终止.相反,它们会闲逛以便重新使用,这样就不会再花费启动时间了.

That's one of the reasons for using a thread pool: the threads aren't terminated once their work is done. Instead, they hang around to be reused so that the start-up time isn't incurred again.

因此,从某种意义上讲,运行时间长的线程是其运行时间远远大于启动它所需的时间的线程.在这种情况下,启动时间远不如运行短线程重要.

So, in that sense, a long running thread is one whose run time is far greater than the time required to start it. In that case, the start-up time is far less important than it is for short running threads.

相反,短运行线程是指运行时间小于或等于启动时间的线程.

Conversely, short running threads are ones whose run time is less than or comparable to the start-up time.

特别是对于.NET,它的操作有点不同.一旦达到线程的最小数量,线程池代码将尝试将线程创建限制为每半秒一个.

For .NET specifically, it's a little different in operation. The thread pooling code will, once it's reached the minimum number of threads, attempt to limit thread creation to one per half-second.

因此,如果您知道线程将长时间运行,则应通知调度程序,以便它可以相应地进行调整.这可能 意味着只创建一个 new 线程而不是从池中获取一个线程,这样就可以将池留给预期的短期任务使用(不能保证这种行为,但这样做是有意义的.)

Hence, if you know your thread is going to be long running, you should notify the scheduler so that it can adjust itself accordingly. This will probably mean just creating a new thread rather than grabbing one from the pool, so that the pool can be left to service short-running tasks as intended (no guarantees on that behaviour but it would make sense to do it that way).

但是,这并不会改变长期运行和短期运行的含义,这意味着存在一些阈值可以区分两者之间.对于.NET,我建议半秒的数字是一个不错的选择.

However, that doesn't change the meaning of long-running and short-running, all it means is that there's some threshold at which it makes sense to distinguish between the two. For .NET, I would suggest the half-second figure would be a decent choice.

这篇关于什么是“长时间运行的任务"?意思是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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