平行是否与调用多个任务相同。
task.run
:排队在threadpool
上运行的指定工作并返回任务或task< tresult>
为此工作。
Parallel.Invoke
:可能并行执行每个提供的操作。
有效地,他们从线程池中旋转了一个新线程。那么Parallel.Invoke
与调用多个task.run
s相同吗?
Task.Run
: Queues the specified work to run on the ThreadPool
and returns a task or Task<TResult>
handle for that work.
Parallel.Invoke
: Executes each of the provided actions, possibly in parallel.
Effectively they spin up a new thread from the thread pool. So is Parallel.Invoke
same as calling multiple Task.Run
s?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,您想比较这两种技术:
有一个相似之处和两个区别。相似之处是在两种情况下
在
Parallel.Invoke
/task.waitall
返回之前,将调用和完成所有操作。某些动作是否失败都没关系。不支持失败策略。这两个区别是:
Parallel.Invoke 将当前线程用作工作线程之一。相反,
task.waitall
+task.run
专门使用threadpool
threads。当工作线程正常工作时,当前线程无需任何操作。可以使用特定的
maxDegreeofParallelism
,taskscheduler
和concellation> concellationToken
(paralleloptions
)。task.run
不可能。task.factory.startnew
可能是可能的,但是相对繁琐。So you want to compare these two techniques:
There is one similarity and two differences. The similarity is that in both cases
all actions will be invoked and completed before the
Parallel.Invoke
/Task.WaitAll
returns. It doesn't matter if some of the actions fail. There is no support for a fail-fast strategy.The two differences are:
The
Parallel.Invoke
uses the current thread as one of the worker threads. On the contrary theTask.WaitAll
+Task.Run
uses exclusivelyThreadPool
threads. While the worker threads are working, the current thread is blocked doing nothing.The
Parallel.Invoke
can be configured with a specificMaxDegreeOfParallelism
,TaskScheduler
andCancellationToken
(ParallelOptions
). This is not possible with theTask.Run
. It is possible with theTask.Factory.StartNew
, but comparatively it's quite cumbersome.