Class: Concurrent::PerThreadExecutor

Inherits:
Object
  • Object
show all
Includes:
Executor
Defined in:
lib/concurrent/executor/per_thread_executor.rb

Overview

Note:

Intended for use primarily in testing and debugging.

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.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Executor

#can_overflow?, #serialized?

Constructor Details

#initializePerThreadExecutor

Creates a new executor



22
23
24
25
26
# File 'lib/concurrent/executor/per_thread_executor.rb', line 22

def initialize
  @running = Concurrent::AtomicBoolean.new(true)
  @stopped = Concurrent::Event.new
  @count = Concurrent::AtomicFixnum.new(0)
end

Class Method Details

.<<(task) ⇒ self

Submit a task to the executor for asynchronous processing.

Parameters:

  • task (Proc)

    the asynchronous task to perform

Returns:

  • (self)

    returns itself



39
40
41
42
# File 'lib/concurrent/executor/per_thread_executor.rb', line 39

def self.<<(task)
  post(&task)
  self
end

.post(*args) { ... } ⇒ Boolean

Submit a task to the executor for asynchronous processing.

Parameters:

  • args (Array)

    zero or more arguments to be passed to the task

Yields:

  • the asynchronous task to perform

Returns:

  • (Boolean)

    ‘true` if the task is queued, `false` if the executor is not running

Raises:

  • (ArgumentError)

    if no task is given



29
30
31
32
33
34
35
36
# File 'lib/concurrent/executor/per_thread_executor.rb', line 29

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

Instance Method Details

#<<(task) ⇒ self

Submit a task to the executor for asynchronous processing.

Parameters:

  • task (Proc)

    the asynchronous task to perform

Returns:

  • (self)

    returns itself



61
62
63
64
# File 'lib/concurrent/executor/per_thread_executor.rb', line 61

def <<(task)
  post(&task)
  self
end

#killObject

Begin an immediate shutdown. In-progress tasks will be allowed to complete but enqueued tasks will be dismissed and no new tasks will be accepted. Has no additional effect if the thread pool is not running.



89
90
91
92
93
# File 'lib/concurrent/executor/per_thread_executor.rb', line 89

def kill
  @running.make_false
  @stopped.set
  true
end

#post(*args) { ... } ⇒ Boolean

Submit a task to the executor for asynchronous processing.

Parameters:

  • args (Array)

    zero or more arguments to be passed to the task

Yields:

  • the asynchronous task to perform

Returns:

  • (Boolean)

    ‘true` if the task is queued, `false` if the executor is not running

Raises:

  • (ArgumentError)

    if no task is given



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/concurrent/executor/per_thread_executor.rb', line 45

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?Boolean

Is the executor running?

Returns:

  • (Boolean)

    ‘true` when running, `false` when shutting down or shutdown



67
68
69
# File 'lib/concurrent/executor/per_thread_executor.rb', line 67

def running?
  @running.true?
end

#shutdownObject

Begin an orderly shutdown. Tasks already in the queue will be executed, but no new tasks will be accepted. Has no additional effect if the thread pool is not running.



82
83
84
85
86
# File 'lib/concurrent/executor/per_thread_executor.rb', line 82

def shutdown
  @running.make_false
  @stopped.set if @count.value == 0
  true
end

#shutdown?Boolean

Is the executor shutdown?

Returns:

  • (Boolean)

    ‘true` when shutdown, `false` when shutting down or running



77
78
79
# File 'lib/concurrent/executor/per_thread_executor.rb', line 77

def shutdown?
  @stopped.set?
end

#shuttingdown?Boolean

Is the executor shuttingdown?

Returns:

  • (Boolean)

    ‘true` when not running and not shutdown, else `false`



72
73
74
# File 'lib/concurrent/executor/per_thread_executor.rb', line 72

def shuttingdown?
  @running.false? && ! @stopped.set?
end

#wait_for_termination(timeout = nil) ⇒ Boolean

Note:

Does not initiate shutdown or termination. Either ‘shutdown` or `kill` must be called before this method (or on another thread).

Block until executor shutdown is complete or until ‘timeout` seconds have passed.

Parameters:

  • timeout (Integer) (defaults to: nil)

    the maximum number of seconds to wait for shutdown to complete

Returns:

  • (Boolean)

    ‘true` if shutdown complete or false on `timeout`



96
97
98
# File 'lib/concurrent/executor/per_thread_executor.rb', line 96

def wait_for_termination(timeout = nil)
  @stopped.wait(timeout)
end