任务与 AsParallel()
在 Stephen Toub 的书的第 33 页
http://www.microsoft.com/ download/en/details.aspx?id=19222
有代码
var pings = from addr in addrs.AsParallel().WithDegreeOfParallelism(16)
select new Ping().Send(addr);
foreach (var ping in pings)
Console.WriteLine("{0}: {1}", ping.Status, ping.Address);
,根据 Stephen 的说法,更好的版本
var pings = (from addr in addrs
select new Ping().SendTask(addr, null)).ToArray();
Task.WaitAll(pings);
foreach (Task<PingReply> ping in pings)
Console.WriteLine("{0}: {1}", ping.Result.Status, ping.Result.Address);
Stephen 说第二个选项更好,因为“任务抽象也可以用于表示 I/O 绑定操作,并且无需占用线程 但是任务
不是只使用底层的线程池(因此只是使用线程)吗?所以你实际上是在占用一个线程?
On page 33 of Stephen Toub's book
http://www.microsoft.com/download/en/details.aspx?id=19222
There is the code
var pings = from addr in addrs.AsParallel().WithDegreeOfParallelism(16)
select new Ping().Send(addr);
foreach (var ping in pings)
Console.WriteLine("{0}: {1}", ping.Status, ping.Address);
and according to Stephen the better version
var pings = (from addr in addrs
select new Ping().SendTask(addr, null)).ToArray();
Task.WaitAll(pings);
foreach (Task<PingReply> ping in pings)
Console.WriteLine("{0}: {1}", ping.Result.Status, ping.Result.Address);
Stephen says the 2nd option is better because "Task abstraction can also be used to represent I/O-bound operations and without tying up a thread in the
process."
But doesn't a Task just use the Threadpool(hence just using threads anyway) underneath anyway? So you are in fact tying up a thread?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
并非所有任务都代表要在线程上完成的工作。几乎任何从
TaskCompletionSource
返回的任务都代表一些“其他”的东西。如果我们深入研究SendTask
方法,我们会发现它调用SentTaskCore
:所以,不,它不会阻塞线程 - 它使用异步完成机制来避免占用一个线程。线。
来自
TaskCompletionSource
上的文档:因此,正如它所说,它支持未绑定到委托的
Task
- 它允许您将一个Task
交给某人,然后编排该任务的完成方式,当完成涉及除执行委托之外的其他事情时。Not all tasks represent work to be done on a thread. Just about any task returned from
TaskCompletionSource
represents something "other". And if we delve into theSendTask
method, we find it callsSentTaskCore
:So, no, it's not blocking a thread - it's using the async completion mechanisms to avoid tying up a thread.
From the docs on
TaskCompletionSource
:So, as it says, it supports a
Task
that isn't bound to a delegate - it allows you to hand someone aTask
, and then orchestrate how that task gets completed, when completion involves something other than executing a delegate.