是否有.NET通用对象池? [英] Is there a general-purpose object pool for .NET?

查看:227
本文介绍了是否有.NET通用对象池?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,它是昂贵的建造,在时间和内存方面。我想保持这些东西一个游泳池和分配出来的需求,多个线程在同一进程。

I have a class that is expensive to construct, in terms of time and memory. I'd like to maintain a pool of these things and dispense them out on demand to multiple threads in the same process.

有没有办法,已经经过测试和验证的通用对象池? (我不想COM +池。)

Is there a general-purpose object pool that is already tested and proven? (I don't want COM+ pooling.)

推荐答案

没有Cheeso,没有通用的对象池这样。但是,这是一个好主意。我认为这将是pretty的简单开发。关键的一点是使它在一个线程环境中很好地工作。

No Cheeso, there is no general object pool like this. But it is a good idea. I think this would be pretty simple to develop. The key thing is making it work well in a threaded environment.

我觉得这是一个有趣的设计问题。例如,如果需要扩展上断绝级硬件 - 和 - 你将要indivudual线程放弃对象通常,你可以这样做:

I think this is an interesting design problem. For example, if this needs to to scale on sever class hardware -and- you will give objects to indivudual threads often then you might do this:

  1. 保留对象的一个​​水池中央。
  2. 在保持每个线程池(缓存)的填充时,其所谓的第一次为一个线程,当它变空。

此方法可避免每个线程争对大多数的请求。

This way, you avoid per-thread contention for most requests.

不同操作条件下会带你到一个不同的设计。例如,如果对象分配是罕见的或线程数低,那么它可能是更简单的只是为了有围绕集合的锁。这将不能很好地扩展,但在这种情况下,就需要

Different operational conditions would lead you to a different design. For example, if object allocations are rare or the number of threads is low, then it might be simpler just to have a lock around a collection. This won't scale well, but in this case, it would need to.

如果你正确地设计的类或接口,那么你可以改变实施一段时间来处理更复杂的情况。

If you design the class or interface correctly, then you could change the implementation over time to handle more complex scenarios.

这篇关于是否有.NET通用对象池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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