且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否有.NET通用对象池?

更新时间:2023-11-04 12:05:58

没有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.