递归调用方法(客体重用的目的) [英] recursively calling method (for object reuse purpose)
问题描述
我有含有大量的字段(10+),一个巨大的数组(100KB)和一些非托管资源的一个相当大的类。让我举例说明
I have a rather large class which contains plenty of fields (10+), a huge array (100kb) and some unmanaged resources. Let me explain by example
class ResourceIntensiveClass
{
private object unmaganedResource; //let it be the expensive resource
private byte[] buffer = new byte[1024 * 100]; //let it be the huge managed memory
private Action<ResourceIntensiveClass> OnComplete;
private void DoWork(object state)
{
//do long running task
OnComplete(this); //notify callee that task completed so it can reuse same object for another task
}
public void Start(object dataRequiredForCurrentTask)
{
ThreadPool.QueueUserWorkItem(DoWork); //initiate long running work
}
}
的问题是,启动方法的第10000迭代导致堆栈溢出后永不再来。我可以在另一个线程提供了Start方法返回一个机会执行的onComplete代表,但它需要使用额外的CPU时间和资源,你也知道。那么,什么是对我最好的选择?
The problem is that the start method never returns after the 10000th iteration causing a stack overflow. I could execute the OnComplete delegate in another thread giving a chance for the Start method to return, but it requires using extra cpu time and resources as you know. So what is the best option for me?
推荐答案
有一个很好的理由递归做你的计算?这似乎是一个简单的循环会做的伎俩,从而避免了令人难以置信的深筹码的需要。这样的设计似乎你是依靠主()设置你的递归尤其是有问题的。
Is there a good reason for doing your calculations recursively? This seems like a simple loop would do the trick, thus obviating the need for incredibly deep stacks. This design seems especially problematic as you are relying on main() to setup your recursion.
这篇关于递归调用方法(客体重用的目的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!