Class: ThreadPool

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

Overview

Hooray

Defined Under Namespace

Classes: Executor

Instance Method Summary collapse

Constructor Details

#initialize(count) ⇒ ThreadPool

Initialize with number of threads to run



26
27
28
29
30
31
# File 'lib/thread_pool.rb', line 26

def initialize(count)
  @executors = []
  @queue = []
  @count = count    
  count.times { @executors << Executor.new(@queue) }
end

Instance Method Details

#closeObject

Kills all threads



49
50
51
# File 'lib/thread_pool.rb', line 49

def close
  @executors.each {|e| e.close }
end

#execute(&block) ⇒ Object

Runs the block at some time in the near future



34
35
36
# File 'lib/thread_pool.rb', line 34

def execute(&block)
  @queue << block 
end

#joinObject

Sleeps and blocks until the task queue is finished executing



54
55
56
# File 'lib/thread_pool.rb', line 54

def join
  sleep 0.01 until @queue.empty? && @executors.all?{|e| !e.active}
end

#sizeObject

Size of the thread pool



44
45
46
# File 'lib/thread_pool.rb', line 44

def size
  @count
end

#waitingObject

Size of the task queue



39
40
41
# File 'lib/thread_pool.rb', line 39

def waiting
  @queue.size
end