Can using async-await give you any performance benefits?

前端 未结 6 1483
滥情空心
滥情空心 2020-12-14 01:41

Whenever I read about async-await, the use case example is always one where there\'s a UI that you don\'t want to freeze. Either a

6条回答
  •  醉酒成梦
    2020-12-14 02:15

    The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread. The method runs on the current synchronization context and uses time on the thread only when the method is active. You can use Task.Run to move CPU-bound work to a background thread, but a background thread doesn't help with a process that's just waiting for results to become available.

    The async-await functionality of .NET is not different from that of other frameworks. It does not give a performance advantage in local computations, but it just allows continuous switching between tasks in a single thread instead of letting one task blocking the thread. If you want a performance gain for local computations, use Task Parallel Library.

    Visit https://msdn.microsoft.com/en-us/library/dd460717(v=vs.110).aspx

提交回复
热议问题