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:
默认情况下,CLR在池化线程上运行任务,这是短期运行计算绑定工作的理想选择。对于长时间运行和阻塞操作,可以按如下方式阻止使用池化线程:
Task task = Task.Factory.StartNew (() => ..., TaskCreationOptions.LongRunning);
I am reading topic about thread
and task
. Can you explain to me what are "long[er]-running" and "short-running" tasks?
我正在阅读有关线程和任务的主题。你能解释一下“长[呃] - 跑”和“短跑”的任务是什么?
1 个解决方案
#1
11
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. The thread pooling code will, once it's reached the minimum number of threads, attempt to limit thread creation to one per half-second.
特别是对于.NET,它在操作上有点不同。线程池代码一旦达到最小线程数,就会尝试将线程创建限制为每半秒一个。
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,我建议半秒数字将是一个不错的选择。
#1
11
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. The thread pooling code will, once it's reached the minimum number of threads, attempt to limit thread creation to one per half-second.
特别是对于.NET,它在操作上有点不同。线程池代码一旦达到最小线程数,就会尝试将线程创建限制为每半秒一个。
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,我建议半秒数字将是一个不错的选择。