Class: ThreadPool::Worker
- Inherits:
-
Object
- Object
- ThreadPool::Worker
- Defined in:
- lib/thread_pool.rb
Instance Method Summary collapse
- #busy? ⇒ Boolean
- #get_block ⇒ Object
-
#initialize ⇒ Worker
constructor
A new instance of Worker.
- #reset_block ⇒ Object
- #set_block(block) ⇒ Object
Constructor Details
#initialize ⇒ Worker
Returns a new instance of Worker.
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/thread_pool.rb', line 6 def initialize @mutex = Mutex.new @thread = Thread.new do while true sleep 0.001 block = get_block if block block.call reset_block end end end end |
Instance Method Details
#busy? ⇒ Boolean
35 36 37 |
# File 'lib/thread_pool.rb', line 35 def busy? @mutex.synchronize {!@block.nil?} end |
#get_block ⇒ Object
20 21 22 |
# File 'lib/thread_pool.rb', line 20 def get_block @mutex.synchronize {@block} end |
#reset_block ⇒ Object
31 32 33 |
# File 'lib/thread_pool.rb', line 31 def reset_block @mutex.synchronize {@block = nil} end |
#set_block(block) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/thread_pool.rb', line 24 def set_block(block) @mutex.synchronize do raise RuntimeError, "Thread already busy." if @block @block = block end end |