如果线程正在等待异步操作完成,则.NET Task线程的资源是否会暂时返回到池中? [英] Are a .NET Task thread's resources returned back to the pool temporarily if the thread is waiting on an async operation to finish?

查看:119
本文介绍了如果线程正在等待异步操作完成,则.NET Task线程的资源是否会暂时返回到池中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个执行两件事的TPL任务.首先,它调用Web服务.其次,它将一些数据插入数据库.我一次执行多达20个任务,一次又一次地执行相同的操作.他们整天要做的就是调用Web服务并将数据插入数据库中.

I have a TPL Task that does two things. First, it calls a web service. Second, it inserts some data into a database. I have up to 20 Tasks started at one time doing this same thing over and over again. All they do all day is call web services and insert data into a database.

我对.NET中的TPL还是很陌生.我已经使用后台工作进程和异步Web服务完成了一些工作.

I'm fairly new to TPL in .NET. I've done some stuff with background worker processes and async web services.

Web服务调用和数据库插入都阻塞了任务运行所在线程中的调用.

The web service call and the database insert are both blocking calls within the thread the Task is running in.

我了解到,在幕后,当您使用Tasks时,.NET会为您管理线程池.是吗?

I understand that under the covers, when you use Tasks, .NET manages a thread pool for you. Yes?

如果我使用async和await()进行服务调用和数据库调用,而不是使它们阻塞调用,线程池是否会有更多线程可供使用?

Would the thread pool have more threads at its disposal if I made the service call and database call with async and await() instead of making them blocking calls?

我的理论(并且我不确定为什么会这样)是线程在等待阻塞的Web服务时正忙于无所事事,并且无法将其资源临时返回到池中.但是我不知道任务是否在等待异步调用完成,以便在等待时主任务线程是否能够切换到其他进程进行处理.

My theory (and I'm not sure why I think this) is that the thread is busy doing nothing while waiting on the blocking web service and can't return its resources temporarily to the pool. But I wonder if the Tasks were waiting for async calls to finish whether the main Task thread would be able to switch to let other stuff process while waiting.

我的理论正确吗?还是我在捏东西?

Is my theory right? Or am I making stuff up?

我正在使用c#和.NET 4.0,但是如果需要,我可以转到4.5.

I'm using c# and .NET 4.0, but I could go to 4.5 if needed.

推荐答案

如果我将线程池设为 使用async和await()而不是服务调用和数据库调用 让他们阻止通话?

Would the thread pool have more threads at its disposal if I made the service call and database call with async and await() instead of making them blocking calls?

这取决于您所说的利用异步等待"是什么意思..

在使用Task.Run时,在后台,Task类使用ThreadPool使用ThreadPool线程卸载工作.

When you use Task.Run, behind the scenes, the Task class uses the ThreadPool to offload work using a ThreadPool thread.

如果您的服务没有公开真正的异步api,并且您使用Task.Run将工作排队,则无论使用async-await还是什么,您都将仍然阻塞线程池线程来进行IO绑定工作.在您的问题中,您声明两个调用都在阻止调用,在这种情况下,答案是否定的,用于进行这些阻止调用的线程池线程仍将被阻止.

If your service doesn't expose a true async api and you uses Task.Run to queue your work, you will still be blocking a threadpool thread to do IO bound work, regardless of the use of async-await. In your question you state that both calls are blocking calls, and in that case the answer is no, the threadpool thread used to make those blocking calls woul still be blocked.

如果您的服务和数据库调用是真正的异步API(一个不消耗任何额外线程来完成其工作),则可以利用async-await的优势,就像您对其中一个调用await一样(和您根本不需要使用Task.Run),当前线程将把控制权交还给调用者,并且可以在此期间使用它来做更多的工作.如果是这样,那么可以.

If your service and database calls were true async APIs (one that doesn't consume any extra threads to do its work), you could advantage of async-await, as when you await on one of those calls (and you shouldn't need to use Task.Run with them at all), the current thread will yield control back to the caller, and can be used in the meanwhile to do more work. If this is the case, then yes.

我的理论(并且我不确定为什么会这样)是线程在等待阻塞的Web服务时正忙于无所事事,并且无法将其资源临时返回到池中.但是我不知道任务是否在等待异步调用完成,以便在等待时主任务线程是否能够切换到其他进程进行处理.

My theory (and I'm not sure why I think this) is that the thread is busy doing nothing while waiting on the blocking web service and can't return its resources temporarily to the pool. But I wonder if the Tasks were waiting for async calls to finish whether the main Task thread would be able to switch to let other stuff process while waiting.

您的理论是正确的.如果排队的线程池工作的主要工作是发出IO绑定请求,则它大部分时间的花费只会阻塞直到请求完成.

Your theory is correct. If the main job of the queued threadpool work is to make an IO bound request then its spending of most its time simply blocking until the request finishes.

当您await一个Task时,控制权会退回到调用方.假设您的服务调用是REST调用,则可以使用HttpClient公开真正的非线程消耗异步方法,例如GetAsyncPostAsync,并且当您使用await这些调用时,调用线程将释放给同时做更多的工作.

When you await a Task, control yields back to caller. Lets assume your service call was a REST call, you could use HttpClient which exposes true non-thread consuming async methods such as GetAsync, PostAsync, and when you await these calls, your calling thread is released to do more work in the meanwhile.

这篇关于如果线程正在等待异步操作完成,则.NET Task线程的资源是否会暂时返回到池中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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