简体   繁体   English

“长期任务”是什么意思?

[英]What does “long-running tasks” mean?

By default, the CLR runs tasks on pooled threads, which is ideal for short-running compute-bound work. 默认情况下,CLR在池化线程上运行任务,这对于短期运行的计算绑定工作是理想的。 For longer-running and blocking operations, you can prevent use of a pooled thread as follows: 对于长时间运行和阻塞操作,可以按如下方式阻止使用池化线程:

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

I am reading topic about thread and task . 我正在阅读有关threadtask主题。 Can you explain to me what are "long[er]-running" and "short-running" tasks? 你能向我解释什么是“长[ER] - 运行”和“短期运行”的任务?

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. 相反,短运行线程的运行时间小于或等于启动时间。


For .NET specifically, it's a little different in operation. 特别是对于.NET,它在操作上有点不同。 The thread pooling code will, once it's reached the minimum number of threads, attempt to limit thread creation to one per half-second. 线程池代码一旦达到最小线程数,就会尝试将线程创建限制为每半秒一个。

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). 可能意味着只创建一个线程而不是从池中获取一个线程,这样就可以让池按预期为短时间运行的任务提供服务(不保证该行为,但这样做是有意义的)。

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. 对于.NET,我建议半秒数字将是一个不错的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM