Class: Concurrent::FixedThreadPool::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/concurrent/fixed_thread_pool/worker.rb

Instance Method Summary collapse

Constructor Details

#initialize(queue, parent) ⇒ Worker

Returns a new instance of Worker.



9
10
11
12
13
# File 'lib/concurrent/fixed_thread_pool/worker.rb', line 9

def initialize(queue, parent)
  @queue = queue
  @parent = parent
  @mutex = Mutex.new
end

Instance Method Details

#dead?Boolean

Returns:



15
16
17
18
19
# File 'lib/concurrent/fixed_thread_pool/worker.rb', line 15

def dead?
  return @mutex.synchronize do
    @thread.nil? ? false : ! @thread.alive?
  end
end

#killObject



21
22
23
24
25
26
# File 'lib/concurrent/fixed_thread_pool/worker.rb', line 21

def kill
  @mutex.synchronize do
    Thread.kill(@thread) unless @thread.nil?
    @thread = nil
  end
end

#run(thread = Thread.current) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/concurrent/fixed_thread_pool/worker.rb', line 28

def run(thread = Thread.current)
  @mutex.synchronize do
    raise StandardError.new('already running') unless @thread.nil?
    @thread = thread
  end

  loop do
    task = @queue.pop
    if task == :stop
      @thread = nil
      @parent.on_worker_exit(self)
      break
    end

    @parent.on_start_task(self)
    begin
      task.last.call(*task.first)
    rescue
      # let it fail
    ensure
      @parent.on_end_task(self)
    end
  end
end