vb.net - Handle more than 64 thread at the same time -


i reading tutorial thread pooling in vb. there example fibonacci calculations:

imports system.threading  module module1  public class fibonacci     private _n integer     private _fibofn     private _doneevent manualresetevent      public readonly property n() integer                     return _n         end     end property      public readonly property fibofn() integer                     return _fibofn         end     end property      sub new(byval n integer, byval doneevent manualresetevent)         _n = n         _doneevent = doneevent     end sub      ' wrapper method use thread pool.     public sub threadpoolcallback(byval threadcontext object)         dim threadindex integer = ctype(threadcontext, integer)         console.writeline("thread {0} started...", threadindex)         _fibofn = calculate(_n)         console.writeline("thread {0} result calculated...", threadindex)         _doneevent.set()     end sub      public function calculate(byval n integer) integer         if n <= 1             return n         end if         return calculate(n - 1) + calculate(n - 2)     end function  end class   <mtathread()>  sub main()     const fibonaccicalculations integer = 9 ' 0 9      ' 1 event used each fibonacci object     dim doneevents(fibonaccicalculations) manualresetevent     dim fibarray(fibonaccicalculations) fibonacci     dim r new random()      ' configure , start threads using threadpool.     console.writeline("launching {0} tasks...", fibonaccicalculations)      integer = 0 fibonaccicalculations         doneevents(i) = new manualresetevent(false)         dim f = new fibonacci(r.next(20, 40), doneevents(i))         fibarray(i) = f         threadpool.queueuserworkitem(addressof f.threadpoolcallback, i)     next      ' wait threads in pool calculate.     waithandle.waitall(doneevents)     console.writeline("all calculations complete.")      ' display results.     integer = 0 fibonaccicalculations         dim f fibonacci = fibarray(i)         console.writeline("fibonacci({0}) = {1}", f.n, f.fibofn)     next end sub  end module 

i've start module , works correctly , handle 9 calculations:

const fibonaccicalculations integer = 9 

i've increase limits, can handle 63 calculations. 64th calculation exception raised said:

waithandle must less 64

i application handle n calculations. idea set cap pool of threads (for instance: 6). n calculations handle using @ 6 thread @ once. how edit code handle removing waithandle error?

the winapi restriction on number of handles can wait on @ same time pretty hard one. not necessary, you'll exact same outcome if wait each individual one:

' wait threads in pool calculate. integer = 0 fibonaccicalculations     doneevents(i).waitone()   next 

and note how can combine next loop, making program more efficient since overlap calculation display. want favor instead:

' display results. integer = 0 fibonaccicalculations     doneevents(i).waitone()     dim f fibonacci = fibarray(i)     console.writeline("fibonacci({0}) = {1}", f.n, f.fibofn) next 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -