class Concurrent::SimpleExecutorService

An executor service in which every operation spawns a new, independently operating thread.

This is perhaps the most inefficient executor service in this library. It exists mainly for testing an debugging. Thread creation and management is expensive in Ruby and this executor performs no resource pooling. This can be very beneficial during testing and debugging because it decouples the using code from the underlying executor implementation. In production this executor will likely lead to suboptimal performance.

@note Intended for use primarily in testing and debugging.

Public Class Methods

<<(task) click to toggle source

@!macro executor_service_method_left_shift

# File lib/concurrent-ruby/concurrent/executor/simple_executor_service.rb, line 34
def self.<<(task)
  post(&task)
  self
end
post(*args) { |*args| ... } click to toggle source

@!macro executor_service_method_post

# File lib/concurrent-ruby/concurrent/executor/simple_executor_service.rb, line 24
def self.post(*args)
  raise ArgumentError.new('no block given') unless block_given?
  Thread.new(*args) do
    Thread.current.abort_on_exception = false
    yield(*args)
  end
  true
end

Public Instance Methods

<<(task) click to toggle source

@!macro executor_service_method_left_shift

# File lib/concurrent-ruby/concurrent/executor/simple_executor_service.rb, line 56
def <<(task)
  post(&task)
  self
end
kill() click to toggle source

@!macro executor_service_method_kill

# File lib/concurrent-ruby/concurrent/executor/simple_executor_service.rb, line 84
def kill
  @running.make_false
  @stopped.set
  true
end
post(*args) { |*args| ... } click to toggle source

@!macro executor_service_method_post

# File lib/concurrent-ruby/concurrent/executor/simple_executor_service.rb, line 40
def post(*args, &task)
  raise ArgumentError.new('no block given') unless block_given?
  return false unless running?
  @count.increment
  Thread.new(*args) do
    Thread.current.abort_on_exception = false
    begin
      yield(*args)
    ensure
      @count.decrement
      @stopped.set if @running.false? && @count.value == 0
    end
  end
end
running?() click to toggle source

@!macro executor_service_method_running_question

# File lib/concurrent-ruby/concurrent/executor/simple_executor_service.rb, line 62
def running?
  @running.true?
end
shutdown() click to toggle source

@!macro executor_service_method_shutdown

# File lib/concurrent-ruby/concurrent/executor/simple_executor_service.rb, line 77
def shutdown
  @running.make_false
  @stopped.set if @count.value == 0
  true
end
shutdown?() click to toggle source

@!macro executor_service_method_shutdown_question

# File lib/concurrent-ruby/concurrent/executor/simple_executor_service.rb, line 72
def shutdown?
  @stopped.set?
end
shuttingdown?() click to toggle source

@!macro executor_service_method_shuttingdown_question

# File lib/concurrent-ruby/concurrent/executor/simple_executor_service.rb, line 67
def shuttingdown?
  @running.false? && ! @stopped.set?
end
wait_for_termination(timeout = nil) click to toggle source

@!macro executor_service_method_wait_for_termination

# File lib/concurrent-ruby/concurrent/executor/simple_executor_service.rb, line 91
def wait_for_termination(timeout = nil)
  @stopped.wait(timeout)
end

Private Instance Methods

ns_initialize(*args) click to toggle source
# File lib/concurrent-ruby/concurrent/executor/simple_executor_service.rb, line 97
def ns_initialize(*args)
  @running = Concurrent::AtomicBoolean.new(true)
  @stopped = Concurrent::Event.new
  @count = Concurrent::AtomicFixnum.new(0)
end