Class: ThreadPool
- Inherits:
-
Object
- Object
- ThreadPool
- Defined in:
- lib/thread_pool.rb
Overview
Hooray
Defined Under Namespace
Classes: Executor
Instance Method Summary collapse
-
#close ⇒ Object
Kills all threads.
-
#execute(&block) ⇒ Object
Runs the block at some time in the near future.
-
#initialize(count) ⇒ ThreadPool
constructor
Initialize with number of threads to run.
-
#join ⇒ Object
Sleeps and blocks until the task queue is finished executing.
-
#size ⇒ Object
Size of the thread pool.
-
#waiting ⇒ Object
Size of the task queue.
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
#close ⇒ Object
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 |
#join ⇒ Object
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 |
#size ⇒ Object
Size of the thread pool
44 45 46 |
# File 'lib/thread_pool.rb', line 44 def size @count end |
#waiting ⇒ Object
Size of the task queue
39 40 41 |
# File 'lib/thread_pool.rb', line 39 def waiting @queue.size end |