Class: Concurrent::SimpleExecutorService — Concurrent

Class List Method List File List

# Class: Concurrent::SimpleExecutorService

Inherits:
  RubyExecutorService
  - Object
  - Concurrent::Synchronization::LockableObject
  - AbstractExecutorService
  - RubyExecutorService
  - Concurrent::SimpleExecutorService
  show all
Defined in:
  lib/concurrent/executor/simple_executor_service.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.

## Constant Summary

## Instance Attribute Summary (collapse)

- - (Symbol) fallback_policy inherited from RubyExecutorService readonly

  The fallback policy in effect.

- - (Symbol) fallback_policy inherited from AbstractExecutorService readonly

  The fallback policy in effect.

## Class Method Summary (collapse)

- + (self) <<(task)

  Submit a task to the executor for asynchronous processing.

- + (Boolean) post(*args) { ... }

  Submit a task to the executor for asynchronous processing.

## Instance Method Summary (collapse)

- - (self) <<(task)

  Submit a task to the executor for asynchronous processing.

- - (undocumented) kill

  Begin an immediate shutdown.

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

  Submit a task to the executor for asynchronous processing.

- - (Boolean) running?

  Is the executor running?.

- - (undocumented) shutdown

  Begin an orderly shutdown.

- - (Boolean) shutdown?

  Is the executor shutdown?.

- - (Boolean) shuttingdown?

  Is the executor shuttingdown?.

- - (Boolean) wait_for_termination(timeout = nil)

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

## Constructor Details

This class inherits a constructor from Concurrent::RubyExecutorService

## Instance Attribute Details

### - (`Symbol`) fallback_policy (readonly) Originally defined in class RubyExecutorService

Returns The fallback policy in effect. Either `:abort`, `:discard`, or `:caller_runs`.

Returns:

- (`Symbol`) —

  The fallback policy in effect. Either `:abort`, `:discard`, or `:caller_runs`.

### - (`Symbol`) fallback_policy (readonly) Originally defined in class AbstractExecutorService

Returns The fallback policy in effect. Either `:abort`, `:discard`, or `:caller_runs`.

Returns:

- (`Symbol`) —

  The fallback policy in effect. Either `:abort`, `:discard`, or `:caller_runs`.

## Class Method Details

### + (`self`) <<(task)

Submit a task to the executor for asynchronous processing.

Parameters:

- task (`Proc`) —

  the asynchronous task to perform

Returns:

- (`self`) —

  returns itself

    31
    32
    33
    34
    # File 'lib/concurrent/executor/simple_executor_service.rb', line 31

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

### + (`Boolean`) post(*args) { ... }

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

    21
    22
    23
    24
    25
    26
    27
    28
    # File 'lib/concurrent/executor/simple_executor_service.rb', line 21

    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

### - (`self`) <<(task)

Submit a task to the executor for asynchronous processing.

Parameters:

- task (`Proc`) —

  the asynchronous task to perform

Returns:

- (`self`) —

  returns itself

    53
    54
    55
    56
    # File 'lib/concurrent/executor/simple_executor_service.rb', line 53

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

### - (`undocumented`) kill

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.

    81
    82
    83
    84
    85
    # File 'lib/concurrent/executor/simple_executor_service.rb', line 81

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

### - (`Boolean`) post(*args) { ... }

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

    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    # File 'lib/concurrent/executor/simple_executor_service.rb', line 37

    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

### - (`Boolean`) running?

Is the executor running?

Returns:

- (`Boolean`) —

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

    59
    60
    61
    # File 'lib/concurrent/executor/simple_executor_service.rb', line 59

    def running?
      @running.true?
    end

### - (`undocumented`) shutdown

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.

    74
    75
    76
    77
    78
    # File 'lib/concurrent/executor/simple_executor_service.rb', line 74

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

### - (`Boolean`) shutdown?

Is the executor shutdown?

Returns:

- (`Boolean`) —

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

    69
    70
    71
    # File 'lib/concurrent/executor/simple_executor_service.rb', line 69

    def shutdown?
      @stopped.set?
    end

### - (`Boolean`) shuttingdown?

Is the executor shuttingdown?

Returns:

- (`Boolean`) —

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

    64
    65
    66
    # File 'lib/concurrent/executor/simple_executor_service.rb', line 64

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

### - (`Boolean`) wait_for_termination(timeout = nil)

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`

    88
    89
    90
    # File 'lib/concurrent/executor/simple_executor_service.rb', line 88

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