为什么达网络线程池仅用于短的时间跨度任务吗? [英] Why .net Threadpool is used only for short time span tasks?

查看:219
本文介绍了为什么达网络线程池仅用于短的时间跨度任务吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过那个.NET线程池是为短的时间内任务(可能不超过3secs更多)很多地方。在所有这些提我还没有找到一个具体的原因,但应该不会被使用。

I've read at many places that .net Threadpool is meant for short time span tasks (may be not more than 3secs). In all these mentioning I've not found a concrete reason why it should be not be used.

甚至有人说,它会导致讨厌的结果,如果我们用了很长一段时间的任务,也导致死锁。

Even some people said that it leads to nasty results if we use for long time tasks and also leads to deadlocks.

有人可以解释它用简单的英语与技术的原因,我们不应该使用线程池长的时间跨度任务?

要具体,我甚至会想给一个场景,想知道为什么线程池不应该在这种情况下它的背后正当理由使用。

To be specific, I would even like to give a scenario and want to to know why ThreadPool should not be used in this scenario with proper reasons behind it.

方案:我需要处理一些成千上万的用户的数据。用户的处理数据被从本地数据库检索,并使用该信息,我需要加工后连接到托管在其他位置和从API响应将被存储在本地数据库中的API。

Scenario: I need to process some thousands of user's data. User's processing data is retrieved from a local database and using that information I need to connect to an API hosted on some other location and the response from API will be stored in the local database after processing it.

如果,如果我使用线程池有20线程限制有人可以解释我的陷阱在这种情况?每个用户的处理时间的范围可以从3秒到1分钟(或更多)。

If someone can explain me pitfalls in this scenario if I use ThreadPool with thread limit of 20? Processing time of each user may range from 3 sec to 1 min (or more).

推荐答案

线程池的一点是要避免上花费的时间创建线程长于所花费的时间的使用的它的情况。通过重用现有线程,我们得到避免的开销。

The point of the threadpool is to avoid the situation where the time spent creating the thread is longer than the time spent using it. By reusing existing threads, we get to avoid that overhead.

缺点是,线程池是一个共享资源:如果你使用一个线程,别的就不行。所以,如果你有大量的长时间运行的任务的,你可能最终与线程池饥饿,甚至可能导致死锁。

The downside is that the threadpool is a shared resource: if you're using a thread, something else can't. So if you have lots of long-running tasks, you could end up with thread-pool starvation, possibly even leading to deadlock.

不要忘了你的应用程序的code可能没有使用线程池的唯一code ...系统code使用了很多了。

Don't forget that your application's code may not be the only code using the thread pool... the system code uses it a lot too.

这听起来像你的也许的想拥有自己的生产者/消费者队列,用小数目的线程处理它。另外,如果你可以跟使用异步API的其他服务,您可能会发现处理的每个位的的计算机将是短暂的。

It sounds like you might want to have your own producer/consumer queue, with a small number of threads processing it. Alternatively, if you could talk to your other service using an asynchronous API, you may find that each bit of processing on your computer would be short-lived.

这篇关于为什么达网络线程池仅用于短的时间跨度任务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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