Class: EasySuite::ThreadPool

Inherits:
Object
  • Object
show all
Defined in:
lib/easysuite/thread.rb

Overview

– ThreadPool ++ Class for thread pooling.

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ ThreadPool

– initialize ++

size

Maximum size of worker threads. If the value is not positive, there is no limit.



15
16
17
18
19
# File 'lib/easysuite/thread.rb', line 15

def initialize(size)
  @size = size
  @queue = Queue.new
  @workers = []
end

Instance Method Details

#execute(&block) ⇒ Object

– execute ++ Execute block on worker thread.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/easysuite/thread.rb', line 25

def execute(&block)
  if (@size > 0)
    while (@queue.size >= @size)
      sleep(0.01)
    end
  end

  @queue.push(block)

  if (@size <= 0) || (@workers.size < @size)
    @workers << create_worker
  end
end

#shutdownObject

– shutdown ++ Wait for active worker threads.



43
44
45
46
47
48
49
50
# File 'lib/easysuite/thread.rb', line 43

def shutdown
  until @queue.empty? && (@queue.num_waiting == @workers.size)
    sleep(0.01)
  end

  @workers.each {|t| t.kill }
  nil
end