Class: ThreadPool::Task
- Inherits:
-
Object
- Object
- ThreadPool::Task
- Defined in:
- lib/threadpool.rb
Instance Attribute Summary collapse
-
#exception ⇒ Object
readonly
Returns the value of attribute exception.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(*args, &callback) ⇒ Task
constructor
A new instance of Task.
- #join ⇒ Object
Constructor Details
#initialize(*args, &callback) ⇒ Task
Returns a new instance of Task.
69 70 71 72 73 74 75 76 77 |
# File 'lib/threadpool.rb', line 69 def initialize(*args, &callback) @args = args @callback = callback @done = false @result = nil @exception = nil @mutex = Monitor.new @cv = @mutex.new_cond end |
Instance Attribute Details
#exception ⇒ Object (readonly)
Returns the value of attribute exception.
67 68 69 |
# File 'lib/threadpool.rb', line 67 def exception @exception end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
67 68 69 |
# File 'lib/threadpool.rb', line 67 def result @result end |
Instance Method Details
#execute ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/threadpool.rb', line 79 def execute begin @result = @callback.call(*@args) rescue Exception => e @exception = e STDERR.puts "Error in thread #{Thread.current} - #{e}" e.backtrace.each { |element| STDERR.puts(element) } end @mutex.synchronize do @done = true @cv.broadcast end end |
#join ⇒ Object
93 94 95 |
# File 'lib/threadpool.rb', line 93 def join @mutex.synchronize { @cv.wait_until { @done } } end |