Thread and Queue

前端 未结 6 1460
情深已故
情深已故 2020-12-24 12:28

I am interested in knowing what would be the best way to implement a thread based queue.

For example:

I have 10 actions which I want to execute with only 4 t

6条回答
  •  天命终不由人
    2020-12-24 13:04

    There area a few gems that implement this pattern for you; parallel, peach,and mine is called threach (or jruby_threach under jruby). It's a drop-in replacement for #each but allows you to specify how many threads to run with, using a SizedQueue underneath to keep things from spiraling out of control.

    So...

    (1..10).threach(4) {|i| do_my_work(i) }
    

    Not pushing my own stuff; there are plenty of good implementations out there to make things easier.

    If you're using JRuby, jruby_threach is a much better implementation -- Java just offers a much richer set of threading primatives and data structures to use.

提交回复
热议问题