异步等待真正的优势是什么? [英] Real advantages of Async-Await?

查看:135
本文介绍了异步等待真正的优势是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早些时候,我已经发布<一个href=\"http://stackoverflow.com/questions/12634551/difference-between-applying-async-await-at-client-and-at-service\">this问题涉及到在客户端或服务应用异步等待。之前做这个问题,因为它是紧密结合的问题走在前面阅读问题。

Earlier i've posted this question related to applying Async-Await at client or at service. Do read the question before going ahead with this question as it is tightly coupled with the question.

根据我测试的code对C#4.0(TPL)和C#5.0的答案(异步 - 等待)。我使用由服务提供的方法的异步和同步版本和比较在每种情况下使用的线程数调用服务。
以下是code我使用来测试使用的资源:

Based on the answer i've tested the code for C# 4.0 (TPL) and C# 5.0 (Async - Await). I am calling the service using async and sync version of the method provided by the service and comparing the number of threads used in each case. Following is the code i'm using to test the resources used:

主要方法

List<Task<string>> tasksList = new List<Task<string>>();
List<int> asyncThreads = new List<int>();
List<int> tplThreads = new List<int>();
Stopwatch watch = new Stopwatch();
watch.Start();

// Call the Async version of the method
for (int i = 0; i < 500; i++)
{
    tasksList.Add(GetNameFromServiceAsync("Input" + i.ToString(), asyncThreads));
}

Task.WaitAll(tasksList.ToArray());

watch.Stop();

foreach (var item in asyncThreads.Distinct())
{
    Console.WriteLine(item);
}

Console.WriteLine("(C# 5.0)Asynchrony Total Threads = " + asyncThreads.Distinct().Count());
Console.WriteLine(watch.ElapsedMilliseconds.ToString());

watch.Restart();

tasksList.Clear();

// Call the normal method
for (int i = 0; i < 500; i++)
{
    tasksList.Add(GetNameFromService("Input" + i.ToString(), tplThreads));
}

Task.WaitAll(tasksList.ToArray());

watch.Stop();

foreach (var item in tplThreads.Distinct())
{
    Console.WriteLine(item);
}

Console.WriteLine("(C# 4.0)TPL Total Threads" + tplThreads.Distinct().Count());

Console.WriteLine(watch.ElapsedMilliseconds.ToString());

异步和同步调用到服务

static async Task<string> GetNameFromServiceAsync(string name, List<int> threads)
{
  Console.WriteLine(" Start Current Thread : " + System.Threading.Thread.CurrentThread.ManagedThreadId);
    var task = await client.GetNameAsync(name);
    threads.Add(System.Threading.Thread.CurrentThread.ManagedThreadId);
   // Console.WriteLine("End GetNameFromServiceAsync Current Thread : " + System.Threading.Thread.CurrentThread.ManagedThreadId);
    return task;
}

static Task<string> GetNameFromService(string name, List<int> threads)
{

    var task = Task<string>.Factory.StartNew(() =>
        {
            threads.Add(System.Threading.Thread.CurrentThread.ManagedThreadId);
         //   Console.WriteLine("GetNameFromService Current Thread : " + System.Threading.Thread.CurrentThread.ManagedThreadId);
            return client.GetName(name);
        });

    return task;
}

现在我的答案工作,并找出结果如下:

Now I've worked on the answer and find out following results:


  • 如果我让500呼叫服务,它使用只有4-5个线程。

  • 的TPL呼吁让周围44-45螺纹。

  • 时间异步调用大约是17 - 18秒。

  • 时间TPL通话大约是42 - 45秒​​。

我要对我的发现了一些反馈,以便它可以给其他社区成员有用。难道我有什么问题,前面??

I want to have some feedback on my findings so that it can be useful to the other community members as well. Is it what the answer of my earlier question??

修改

Q值。我的观察结论是,如果我们使用异步等待,而不是TPL的Task.Factory.startNew,那么它会消耗较少的线程。多大程度上是正确的呢?如果没有,那么什么是正确的方向拿出这样的比较?

Q. My observation concludes that if we use Async-Await instead of TPL's Task.Factory.startNew , then it will consume lesser threads. HOW CORRECT IS THIS? IF NO, THEN WHAT IS THE RIGHT DIRECTION TO COME UP WITH SUCH COMPARISON?

Q值。由于我学习异步 - 等待,我要证明通过某种比较坚实code的它的价值

Q. As i am learning async - await, i want to prove its worth by some sort of comparison and solid code.

推荐答案

客户端异步(相对于同步code)概括在一个很小的提高响应成本<内存/ EM>。

Client-side async (compared to synchronous code) generally improves responsiveness at a slight cost of memory.

服务器端异步(相对于同步code)一般通过减少内存/线程的使用提高可伸缩性。这些优势也适用于客户端异步(相比于多线程code)。

Server-side async (compared to synchronous code) generally improves scalability by reducing memory/thread usage. These advantages also apply to client-side async (compared to multithreaded code).

这两种都是极端的概括也肯定有,他们是错的情况。

Both of these are extreme generalizations and there are certainly situations where they're wrong.

更新:

我的观察结论是,如果我们使用异步等待...,然后它会消耗较少的线程。

My observation concludes that if we use Async-Await ..., then it will consume lesser threads.

异步 / 等待使维护异步code。由自己,他们没有什么关系创建线程。然而,他们经常被用来与 Task.Run (或 Task.Factory.StartNew )来创建后台任务。

async / await enable maintainable asynchronous code. By themselves, they don't have anything to do with creating threads. However, they are often used with Task.Run (or Task.Factory.StartNew) to create background tasks.

由于我学习异步 - 等待,我想通过某种比较坚实code,证明了它的价值。

As i am learning async - await, i want to prove its worth by some sort of comparison and solid code.

异步等待是编译器的转换。他们更容易地编写异步程序 - 这是所有

async and await are compiler transformations. They make it easier to write asynchronous programs - that is all.

如果你把它们比作同步code,那么你通常会看到更好的响应速度和/或可扩展性。如果你把它们比现有的异步code,那么他们通常会是的的效率较低,但超过弥补在code可维护性方面。

If you compare them to synchronous code, then you'll usually see improved responsiveness and/or scalability. If you compare them to existing asynchronous code, then they'll usually be slightly less efficient but more than make up for that in terms of code maintainability.

这篇关于异步等待真正的优势是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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