Task.Factory.StartNew() 是否保证创建至少一个新线程?
我知道 TPL 不一定为并行集中的每个任务创建一个新线程,但它总是至少创建一个吗?例如:
private void MyFunc()
{
Task.Factory.StartNew(() =>
{
//do something that takes a while
});
DoSomethingTimely(); //is this line guaranteed to be hit immediately?
}
编辑:澄清一下:是的,我的意思是保证执行MyFunc()
的线程不会被用来执行//做某事这需要一段时间
。
I understand that the TPL does not necessarily create a new thread for every task in a parallel set, but does it always create at least one? eg:
private void MyFunc()
{
Task.Factory.StartNew(() =>
{
//do something that takes a while
});
DoSomethingTimely(); //is this line guaranteed to be hit immediately?
}
EDIT: To clarify: Yes, I mean is it guaranteed that the thread executing MyFunc()
is not going to be used to execute //do something that takes a while
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这取决于当前默认的
TaskScheduler
是。您可以想象有人做了一些可怕的事情,比如实现一个 SynchronousTaskScheduler,它在 QueueTask 期间执行任务主体,并在返回之前将其设置为完成。假设您不让其他人破坏您的任务计划程序,那么您就不必担心这一点。
That's up to whatever the current default
TaskScheduler
is. You can just about envisage someone doing something horrific like implementing aSynchronousTaskScheduler
that executes the task body duringQueueTask
and sets it to complete before returning.Assuming you're not letting someone else muck about with your task schedulers, you shouldn't have to worry about it.
这取决于您所说的“立即”的含义,但我认为可以合理地假设 TPL 不会劫持您当前正在执行的线程以同步运行任务中的代码(如果您是这样的话)意思是。至少不是使用普通的调度程序...您可能可以编写自己的调度程序来执行此操作,但您通常可以假设
StartNew
将调度 任务而不是仅仅内联运行它。It depends on what you mean by "immediately" but I think it's reasonable to assume that the TPL isn't going to hijack your currently executing thread to synchronously run the code in your task, if that's what you mean. At least not with the normal scheduler... you could probably write your own scheduler which does do so, but you can normally assume that
StartNew
will schedule the task rather than just running it inline.您的主要问题和代码中的问题是完全不同的问题。但这两个问题的答案是:
1)不,不能保证线程会启动。创建并启动的是一个任务。最终,某个线程将必须执行该任务,但是否会创建一个线程尚未指定。可以重用现有的线程。
2)这取决于你所说的“立即”是什么意思。严格来说,时效性没有保证。但你已经告诉系统执行该任务,它至少会在完成它认为更重要的所有事情后立即启动它。不能保证严格的公平性或及时性。
Your main question and the question in your code are completely different questions. But the answers to the two questions are:
1) No, there's no guarantee a thread will be started. What is created and started is a task. Ultimately, some thread will have to execute that task, but whether one will be created is unspecified. An existing thread could be re-used.
2) It depends what you mean by "immediately". Strictly speaking, there is no timeliness guarantee. But you have told the system to execute that task, and it will at least start it as soon as it finishes everything it considers more important. Strict fairness or timeliness is not guaranteed.
是的,在分派任务运行后不久就会出现。
不,它不会创建一个新线程,而是声明一个线程。当他们说它并不总是创建新线程时,他们指的是它重复使用线程池中的线程这一事实。
池的大小基于检测到的 CPU 核心数。但它始终包含至少 1 个线程。 ;)
Yes, it will hit very shortly after dispatching the task to run.
No, it will not create a new thread, but claim one. When they say it doesn't always create a new thread, they are referring to the fact that it re-uses threads from a thread pool.
The size of the pool is based on the number of CPU cores detected. But it will always contain at least 1 thread. ;)
简而言之:是的,这是有保证的。
更长:如果
StartNew()
确实不创建一个新线程,它将重用另一个线程:要么通过空闲,要么通过排队。In short: Yes, this is guranteed.
Longer: If
StartNew()
does not create a new thread, it will reuse another: Either by it being free, or by queueing.DoSomethingTimely 会很快被调用,但这与创建新线程或将任务添加到队列有什么关系呢?
DoSomethingTimely will get called very quickly, but what does that have to do with creating a new thread, or adding the task to a queue?