class Concurrent::RubySingleThreadExecutor

@!macro single_thread_executor

def alive?

@!visibility private
def alive?
  @thread && @thread.alive?
end

def execute(*args, &task)

@!visibility private
def execute(*args, &task)
  supervise
  @queue << [args, task]
end

def initialize(opts = {})

Other tags:
    See: http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html -
    See: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html -
    See: http://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html -
def initialize(opts = {})
  @queue = Queue.new
  @thread = nil
  init_executor
end

def kill_execution

@!visibility private
def kill_execution
  @queue.clear
  @thread.kill if alive?
end

def new_worker_thread

@!visibility private
def new_worker_thread
  Thread.new do
    Thread.current.abort_on_exception = false
    work
  end
end

def shutdown_execution

@!visibility private
def shutdown_execution
  @queue << :stop
  stopped_event.set unless alive?
end

def supervise

@!visibility private
def supervise
  @thread = new_worker_thread unless alive?
end

def work

@!visibility private
def work
  loop do
    task = @queue.pop
    break if task == :stop
    begin
      task.last.call(*task.first)
    rescue => ex
      # let it fail
    end
  end
  stopped_event.set
end