有一些关于在递归函数中使用线程的想法吗 [英] Does some have an Idea for the use of Threading in Recursive functions

查看:203
本文介绍了有一些关于在递归函数中使用线程的想法吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!
谢谢!

我的情况:
我使用VB.NET 3.5,VS2008
我有一棵数据树.我通过应用程序API调用获得了数据.
约:300MB,1100个节点.最大树深为6

我的单线程函数:

Hi Guys!
Thx for reding!

My Situation:
i use VB.NET 3.5, VS2008
i have got an tree of data. i got the data via an app api call.
about: 300MB, 1100 Nodes. max tree deep of 6

my single thread func:

Private Function IterateThruTree(ByVal AktComponet As Component, ByVal AktDeep As Integer) As sBillOfMatiereal
  ...
  For Each lComponet As Component In AktComponet.GetChildren()
                If _bCancle Then Exit Function
                lBoM = IterateThruTree(lComponet, AktDeep + 1)
   ...
   next
...
end funtion



我不会在线程中这样做.我的一个朋友告诉我建立一个类并从线程继承.但它认为这会产生大量开销.我认为线程池会做得更好.
有什么好主意吗?

到目前为止,新年快乐!



i wonne do this in threads. a friend of mine told me to make a class and inherit from thread. but it think this produce a lot of overhead. i think an threadpool would do it better.
any good ideas?

so far, happy new year!

推荐答案

好吧,您可以在类级别存储线程数.每当创建新线程时,就将其添加到计数中.线程完成后,从计数中删除.当达到最大计数时,请等待其减少后再开始新线程.您可能会执行此检查,以在上面的for循环内创建新线程.因此,如果线程数最大,则只需递归调用IterateThruTree.但是,如果线程数小于最大线程数,则将IterateThruTree称为新线程.您需要记住很多事情,例如线程池,资源锁定和返回值,但我将把细节留给您.
Well, you could store a count of threads at the class level. Whenever a new thread is created, add to the count. When the thread is complete, remove from the count. When the maximum count is reached, wait for it to be reduced before starting a new thread. You would likely do this check to make new threads inside the for loop you have above. So, if the threads are at max, then just recursively call IterateThruTree. But if the threads are less than max, then call IterateThruTree as a new thread. There are various things you''ll want to keep in mind, such as thread pooling, resource locking, and returning values, but I''ll leave the details to you.


aspdotnetdev写道:
aspdotnetdev wrote:

因此,如果线程数最大,则只需递归调用IterateThruTree.但是,如果线程数少于最大线程数,则将IterateThruTree称为新线程.

So, if the threads are at max, then just recursively call IterateThruTree. But if the threads are less than max, then call IterateThruTree as a new thread.



这真是个好主意.



Thats an realy good idea, thx.


写道:​​

我不会在线程中这样做.

i wonne do this in threads.

blockquote>

我不建议仅使用多线程来遍历树.这将使代码变得复杂,并且很难维持迭代的顺序.在单线程上进行迭代,并生成新线程进行处理.



I will not recommend to use multi threading just to iterate over the tree. It will make the code complex and it is very tough to maintain the order of iteration. Do the iteration on single thread and spawn new threads for processing.


这篇关于有一些关于在递归函数中使用线程的想法吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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