简体   繁体   English

直接指定动作或使用TPL数据流ActionBlock指定任务生成器有什么区别?

[英]What is the difference between specifying Action directly or Task-Generator with TPL dataflow ActionBlock?

What is the difference between specifying Action directly or Func<Task> with .NET TPL Dataflow ActionBlock ? 直接指定Action或使用.NET TPL Dataflow ActionBlock指定Func<Task>有什么区别?

Direct Action: 直接行动:

new ActionBlock<Message[]>(x => DoSomething(x))

Task: 任务:

new ActionBlock<Message[]>(x => Task.Run(() => DoSomething(x)))

I'm trying to understand the differences in regard to parallel execution ( MaxDegreeOfParallelism > 1). 我试图了解并行执行方面的差异( MaxDegreeOfParallelism > 1)。

TPL Dataflow supports both async and synchronous delegates so there's no difference regarding the degree of parallelism. TPL Dataflow支持async和同步委托,因此并行度没有差异。 It "knows" to await the returned task that represents the item's asynchronous execution and not continue on to the next item. 它“知道” await表示该项目的异步执行的返回任务,而不继续进行下一个项目。 In both cases no more than MaxDegreeOfParallelism items would be processed concurrently. 在这两种情况下, MaxDegreeOfParallelism只能同时处理MaxDegreeOfParallelism项目。 The first option would use the synchronous delegate (ie Action ) while the second the async one (ie Func<Task> ). 第一种选择将使用的同步委托(即, Action ),而第二所述async一个(即Func<Task> )。

However using Task.Run would take up an extra thread for each item's execution only to release it back to the thread pool after it's done. 但是,使用Task.Run将为每个项目的执行占用一个额外的线程,只是在完成后才将其释放回线程池。 It's offloading work to another thread with no reason. 它没有任何理由将工作转移到另一个线程。 So don't use it, it adds no value. 因此,请勿使用它,它不会增加任何价值。

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

相关问题 Task.ContinueWith 和 ActionBlock.LinkTo 之间的确切区别是什么? - What is the exact difference between Task.ContinueWith and ActionBlock.LinkTo? 等待ActionBlock <T> - TPL DataFlow - Awaiting ActionBlock<T> - TPL DataFlow 为什么我的 TPL 数据流操作块没有并行执行? - Why is my TPL Dataflow Actionblock not executing in parallell? 取消数据流 ActionBlock 中的特定任务 - Cancelling specific task in a dataflow ActionBlock System.Threading.Tasks.Dataflow和Microsoft.Tpl.Dataflow之间有什么区别 - What is difference between System.Threading.Tasks.Dataflow and Microsoft.Tpl.Dataflow 如何将 TPL Dataflow TransformBlock 或 ActionBlock 放在单独的文件中? - How to put a TPL Dataflow TranformBlock or ActionBlock in a separate file? TPL数据流转换块发布到批处理块后跟动作块 - TPL Dataflow Transform block post to batch block followed by actionblock TPL Dataflow,Post()和SendAsync()之间的功能区别是什么? - TPL Dataflow, whats the functional difference between Post() and SendAsync()? TPL数据流。 无法将BatchBlock附加到ActionBlock。 无法从用法中推断出类型实参? - TPL Dataflow. Cannot attach BatchBlock to ActionBlock. Type argument cannot be inferred from usage? TPL.Dataflow - 在ActionBlock中发生无法处理的异常时阻止挂起<T> - TPL.Dataflow - preventing hangs when unhanded exception occurs in ActionBlock<T>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM