Class: Concurrent::SimpleExecutorService
- Inherits:
-
RubyExecutorService
- Object
- Concurrent::Synchronization::LockableObject
- AbstractExecutorService
- RubyExecutorService
- Concurrent::SimpleExecutorService
- Defined in:
- lib/concurrent-ruby/concurrent/executor/simple_executor_service.rb
Overview
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
-
.<<(task) ⇒ self
Submit a task to the executor for asynchronous processing.
-
.post(*args) { ... } ⇒ Boolean
Submit a task to the executor for asynchronous processing.
Instance Method Summary collapse
-
#<<(task) ⇒ self
Submit a task to the executor for asynchronous processing.
-
#kill ⇒ undocumented
Begin an immediate shutdown.
-
#post(*args) { ... } ⇒ Boolean
Submit a task to the executor for asynchronous processing.
-
#running? ⇒ Boolean
Is the executor running?.
-
#shutdown ⇒ undocumented
Begin an orderly shutdown.
-
#shutdown? ⇒ Boolean
Is the executor shutdown?.
-
#shuttingdown? ⇒ Boolean
Is the executor shuttingdown?.
-
#wait_for_termination(timeout = nil) ⇒ Boolean
Block until executor shutdown is complete or until
timeout
seconds have passed.
Class Method Details
.<<(task) ⇒ self
Submit a task to the executor for asynchronous processing.
34 35 36 37 |
# File 'lib/concurrent-ruby/concurrent/executor/simple_executor_service.rb', line 34 def self.<<(task) post(&task) self end |
.post(*args) { ... } ⇒ Boolean
Submit a task to the executor for asynchronous processing.
24 25 26 27 28 29 30 31 |
# 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 |
Instance Method Details
#<<(task) ⇒ self
Submit a task to the executor for asynchronous processing.
56 57 58 59 |
# File 'lib/concurrent-ruby/concurrent/executor/simple_executor_service.rb', line 56 def <<(task) post(&task) self end |
#kill ⇒ undocumented
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.
84 85 86 87 88 |
# File 'lib/concurrent-ruby/concurrent/executor/simple_executor_service.rb', line 84 def kill @running.make_false @stopped.set true end |
#post(*args) { ... } ⇒ Boolean
Submit a task to the executor for asynchronous processing.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# 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? ⇒ Boolean
Is the executor running?
62 63 64 |
# File 'lib/concurrent-ruby/concurrent/executor/simple_executor_service.rb', line 62 def running? @running.true? end |
#shutdown ⇒ undocumented
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.
77 78 79 80 81 |
# 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? ⇒ Boolean
Is the executor shutdown?
72 73 74 |
# File 'lib/concurrent-ruby/concurrent/executor/simple_executor_service.rb', line 72 def shutdown? @stopped.set? end |
#shuttingdown? ⇒ Boolean
Is the executor shuttingdown?
67 68 69 |
# File 'lib/concurrent-ruby/concurrent/executor/simple_executor_service.rb', line 67 def shuttingdown? @running.false? && ! @stopped.set? end |
#wait_for_termination(timeout = nil) ⇒ Boolean
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.
91 92 93 |
# File 'lib/concurrent-ruby/concurrent/executor/simple_executor_service.rb', line 91 def wait_for_termination(timeout = nil) @stopped.wait(timeout) end |