Class: Rosarium::FixedThreadExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/rosarium/fixed_thread_executor.rb

Instance Method Summary collapse

Constructor Details

#initialize(max = 1) ⇒ FixedThreadExecutor

Returns a new instance of FixedThreadExecutor.



5
6
7
8
9
10
11
# File 'lib/rosarium/fixed_thread_executor.rb', line 5

def initialize(max = 1)
  @max = max
  @mutex = Mutex.new
  @waiting = []
  @executing = 0
  @threads = []
end

Instance Method Details

#discardObject



24
25
26
# File 'lib/rosarium/fixed_thread_executor.rb', line 24

def discard
  @mutex.synchronize { @waiting.clear }
end

#submit(&block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/rosarium/fixed_thread_executor.rb', line 13

def submit(&block)
  @mutex.synchronize do
    @waiting << block
    if @executing < @max
      @executing = @executing + 1
      t = Thread.new { execute_and_count_down }
      @threads.push t
    end
  end
end

#wait_until_idleObject



28
29
30
31
32
33
34
# File 'lib/rosarium/fixed_thread_executor.rb', line 28

def wait_until_idle
  loop do
    t = @mutex.synchronize { @threads.shift }
    t or break
    t.join
  end
end