Class: PoolParty::ThreadPool::Worker
- Defined in:
- lib/poolparty/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.
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/poolparty/thread_pool.rb', line 4 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
33 34 35 |
# File 'lib/poolparty/thread_pool.rb', line 33 def busy? @mutex.synchronize {!@block.nil?} end |
#get_block ⇒ Object
18 19 20 |
# File 'lib/poolparty/thread_pool.rb', line 18 def get_block @mutex.synchronize {@block} end |
#reset_block ⇒ Object
29 30 31 |
# File 'lib/poolparty/thread_pool.rb', line 29 def reset_block @mutex.synchronize {@block = nil} end |
#set_block(block) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/poolparty/thread_pool.rb', line 22 def set_block(block) @mutex.synchronize do raise RuntimeError, "Thread already busy." if @block @block = block end end |