第三方物流与放大器之间的差异;异步/的await(线程处理) [英] Difference between the TPL & async/await (Thread handling)

查看:84
本文介绍了第三方物流与放大器之间的差异;异步/的await(线程处理)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图理解TPL&放大器之间的差异步,当涉及到线程创建/游览车。

我相信TPL(TaskFactory.Startnew)的工作原理类似于ThreadPool.QueueUserWorkItem,它排队的工作就在线程池中的线程。那是当然的,除非你使用TaskCreationOptions.LongRunning它创建一个新的线程。

我想异步/的await会工作同样如此本质:

TPL:

  Factory.StartNew(()=> DoSomeAsyncWork())
.ContinueWith(
    (先行)= GT; {
        DoSomeWorkAfter();
    },TaskScheduler.FromCurrentSynchronizationContext());

异步/等待:

 等待DoSomeAsyncWork();
DoSomeWorkAfter();

将是相同的。从我一直在读这似乎是异步/只等待有时会创建一个新的线程。因此,当它创建一个新的线程,当没有它创建一个新线程?如果你正在处理IO完成端口,我可以看到它不必创建一个新的线程,但否则我会认为这将不得不这样做。我想我的FromCurrentSynchronizationContext的理解总是有点模糊也。我总是throught它在本质上,UI线程。


解决方案

  

我相信TPL(TaskFactory.Startnew)的工作原理类似于ThreadPool.QueueUserWorkItem,它排队的工作就在线程池中的线程。


pretty多


  

从我一直在读这似乎是异步/只等待有时会创建一个新的线程。


实际上,它从来不会。如果你想多线程,你必须自己实现它。有这还只是针对 Task.Factory.StartNew 速记一个新的 Task.Run 方法,并且它可能是最常见的线程池开始任务的方式。


  

如果你处理IO完成端口,我可以看到它不必创建一个新的线程,但否则我会认为这将不得不这样做。


宾果。因此,像方法 Stream.ReadAsync 将实际创建围绕一个IOCP 工作包装(如有一个IOCP)。

您还可以创建一些非I / O,非CPU任务。一个简单的例子是 Task.Delay ,它返回某个时间段之后完成的任务。

关于很酷的事情异步 / 等待是你可以排队一些工作线程池(例如, Task.Run ),做一些I / O密集​​型操作(例如, Stream.ReadAsync ),并做一些其他操作(例如, Task.Delay )......他们是所有任务!它们可以被期待已久的使用或组合使用,比如 Task.WhenAll

这是返回的任何方法工作的await 编辑 - 这并不一定是异步方法。因此, Task.Delay 和I / O密集​​型操作只需要使用 TaskCompletionSource 以创建并完成任务 - 唯一关于线程池正在做是在事件发生时(超时,I / O完成,等)的实际任务完成


  

我想我FromCurrentSynchronizationContext的理解总是有点模糊也。我总是throught它在本质上,UI线程。


我写的SynchronizationContext C>。在大多数情况下, SynchronizationContext.Current

任何线程的可以的设置自己的的SynchronizationContext ,所以也有例外上述规则。

请注意,默认工作 awaiter将安排在异步方法的其余部分的电流的SynchronizationContext 如果它不为空的;否则它会在当前的TaskScheduler 。这是不那么重要的今天,但在不久的将来这将是一个重要的区别。

我写我自己的<$c$c>async/<$c$c>await引导页我的博客上,和Stephen Toub最近发布了一个极好的<一个href=\"http://blogs.msdn.com/b/pfxteam/archive/2012/04/12/10293335.aspx\"><$c$c>async/<$c$c>await常见问题解答。

关于并发与多线程,见此相关的SO问题。我想说异步允许并发,这可能会或可能不会是多线程的。它易于使用等待Task.WhenAll 等待Task.WhenAny 做并行处理,除非你明确地使用线程池(例如, Task.Run ConfigureAwait(假)),那么你可以有几个正在进行中的并发操作在同一时间(例如,多个I / O或其它类型,如延迟) - 并没有必要对他们的线程。我使用单线程并发为这种情况,虽然在ASP.NET主机,则实际上可以落得的的-threaded并发。这是pretty甜蜜。

Trying to understanding the difference between the TPL & async/await when it comes to thread creation.

I believe the TPL (TaskFactory.Startnew) works similar to ThreadPool.QueueUserWorkItem in that it queues up work on a thread in the thread pool. That's of course unless you use TaskCreationOptions.LongRunning which create a new thread.

I thought async/await would work similarly so essentially:

TPL:

Factory.StartNew( () => DoSomeAsyncWork() )
.ContinueWith( 
    (antecedent) => {
        DoSomeWorkAfter(); 
    },TaskScheduler.FromCurrentSynchronizationContext());

Async/Await:

await DoSomeAsyncWork();  
DoSomeWorkAfter();

would be identical. From what i've been reading it seems like async/await only "sometimes" creates a new thread. So when does it create a new thread and when doesn't it create a new thread? If you were dealing with IO completion ports i can see it not having to create a new thread but otherwise i would think it would have to. I guess my understanding of FromCurrentSynchronizationContext always was a bit fuzzy also. I always throught it was, in essence, the UI thread.

解决方案

I believe the TPL (TaskFactory.Startnew) works similar to ThreadPool.QueueUserWorkItem in that it queues up work on a thread in the thread pool.

Pretty much.

From what i've been reading it seems like async/await only "sometimes" creates a new thread.

Actually, it never does. If you want multithreading, you have to implement it yourself. There's a new Task.Run method that is just shorthand for Task.Factory.StartNew, and it's probably the most common way of starting a task on the thread pool.

If you were dealing with IO completion ports i can see it not having to create a new thread but otherwise i would think it would have to.

Bingo. So methods like Stream.ReadAsync will actually create a Task wrapper around an IOCP (if the Stream has an IOCP).

You can also create some non-I/O, non-CPU "tasks". A simple example is Task.Delay, which returns a task that completes after some time period.

The cool thing about async/await is that you can queue some work to the thread pool (e.g., Task.Run), do some I/O-bound operation (e.g., Stream.ReadAsync), and do some other operation (e.g., Task.Delay)... and they're all tasks! They can be awaited or used in combinations like Task.WhenAll.

Any method that returns Task can be awaited - it doesn't have to be an async method. So Task.Delay and I/O-bound operations just use TaskCompletionSource to create and complete a task - the only thing being done on the thread pool is the actual task completion when the event occurs (timeout, I/O completion, etc).

I guess my understanding of FromCurrentSynchronizationContext always was a bit fuzzy also. I always throught it was, in essence, the UI thread.

I wrote an article on SynchronizationContext. Most of the time, SynchronizationContext.Current:

  • is a UI context if the current thread is a UI thread.
  • is an ASP.NET request context if the current thread is servicing an ASP.NET request.
  • is a thread pool context otherwise.

Any thread can set its own SynchronizationContext, so there are exceptions to the rules above.

Note that the default Task awaiter will schedule the remainder of the async method on the current SynchronizationContext if it is not null; otherwise it goes on the current TaskScheduler. This isn't so important today, but in the near future it will be an important distinction.

I wrote my own async/await intro on my blog, and Stephen Toub recently posted an excellent async/await FAQ.

Regarding "concurrency" vs "multithreading", see this related SO question. I would say async enables concurrency, which may or may not be multithreaded. It's easy to use await Task.WhenAll or await Task.WhenAny to do concurrent processing, and unless you explicitly use the thread pool (e.g., Task.Run or ConfigureAwait(false)), then you can have multiple concurrent operations in progress at the same time (e.g., multiple I/O or other types like Delay) - and there is no thread needed for them. I use the term "single-threaded concurrency" for this kind of scenario, though in an ASP.NET host, you can actually end up with "zero-threaded concurrency". Which is pretty sweet.

这篇关于第三方物流与放大器之间的差异;异步/的await(线程处理)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆