gpt4 book ai didi

c# - 使用 async/await 是否会创建一个新线程?

转载 作者:IT王子 更新时间:2023-10-29 04:00:08 25 4
gpt4 key购买 nike

我是 TPL 的新手我想知道:C# 5.0 新增的异步编程支持(通过新的 asyncawait 关键字)与线程的创建有何关系?

具体来说,async/await 的使用是否会在每次使用时创建一个新线程?如果有许多使用 async/await 的嵌套方法,是否为每个方法创建一个新线程?

最佳答案

简而言之

来自 Asynchronous Programming with Async and Await : Threads

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.

关于c# - 使用 async/await 是否会创建一个新线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27265818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com