Class: MyThreadPool
- Inherits:
-
Object
- Object
- MyThreadPool
- Defined in:
- lib/my_thread_pool.rb,
lib/my_thread_pool/version.rb
Overview
require ‘./my_thread_pool/version’
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Method Summary collapse
-
#initialize(size = 2) ⇒ MyThreadPool
constructor
A new instance of MyThreadPool.
- #join ⇒ Object
- #perform(&block) ⇒ Object
Constructor Details
#initialize(size = 2) ⇒ MyThreadPool
Returns a new instance of MyThreadPool.
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/my_thread_pool.rb', line 4 def initialize(size = 2) @queue = Queue.new @threads = Array.new(size) do Thread.new do id = (rand * 100).to_i while task = @queue.pop and !task.nil? task.call end end end end |
Instance Method Details
#join ⇒ Object
20 21 22 23 |
# File 'lib/my_thread_pool.rb', line 20 def join @threads.size.times { @queue.push nil } @threads.map(&:join) end |
#perform(&block) ⇒ Object
16 17 18 |
# File 'lib/my_thread_pool.rb', line 16 def perform(&block) @queue.push block end |