Class: MarchHare::ThreadPools
- Inherits:
-
Object
- Object
- MarchHare::ThreadPools
- Defined in:
- lib/march_hare/thread_pools.rb
Overview
A slighly more Ruby developer-friendly way of instantiating various JDK executors (thread pools).
Class Method Summary collapse
-
.daemon_thread_factory(base_factory: JavaConcurrent::Executors.default_thread_factory) ⇒ Object
Returns a new thread factory that creates daemon threads.
-
.dynamically_growing(use_daemon_threads: false) ⇒ Object
Returns a new thread pool (JDK executor) that will create new threads as needed.
-
.fixed_of_size(n, use_daemon_threads: false) ⇒ Object
Returns a new thread pool (JDK executor) of a fixed size.
-
.single_threaded(use_daemon_threads: false) ⇒ Object
Returns a new thread pool (JDK executor) of a fixed size of 1.
Class Method Details
.daemon_thread_factory(base_factory: JavaConcurrent::Executors.default_thread_factory) ⇒ Object
Returns a new thread factory that creates daemon threads.
57 58 59 60 61 |
# File 'lib/march_hare/thread_pools.rb', line 57 def self.daemon_thread_factory(base_factory: JavaConcurrent::Executors.default_thread_factory) proc { |runnable| base_factory.new_thread(runnable).tap { |t| t.daemon = true } } end |
.dynamically_growing(use_daemon_threads: false) ⇒ Object
Returns a new thread pool (JDK executor) that will create new threads as needed.
44 45 46 47 48 49 50 |
# File 'lib/march_hare/thread_pools.rb', line 44 def self.dynamically_growing(use_daemon_threads: false) if use_daemon_threads JavaConcurrent::Executors.new_cached_thread_pool(&daemon_thread_factory) else JavaConcurrent::Executors.new_cached_thread_pool end end |
.fixed_of_size(n, use_daemon_threads: false) ⇒ Object
Returns a new thread pool (JDK executor) of a fixed size.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/march_hare/thread_pools.rb', line 14 def self.fixed_of_size(n, use_daemon_threads: false) raise ArgumentError.new("n must be a positive integer!") unless Integer === n raise ArgumentError.new("n must be a positive integer!") unless n > 0 if use_daemon_threads JavaConcurrent::Executors.new_fixed_thread_pool(n, &daemon_thread_factory) else JavaConcurrent::Executors.new_fixed_thread_pool(n) end end |
.single_threaded(use_daemon_threads: false) ⇒ Object
Returns a new thread pool (JDK executor) of a fixed size of 1.
30 31 32 33 34 35 36 |
# File 'lib/march_hare/thread_pools.rb', line 30 def self.single_threaded(use_daemon_threads: false) if use_daemon_threads JavaConcurrent::Executors.new_single_thread_executor(&daemon_thread_factory) else JavaConcurrent::Executors.new_single_thread_executor end end |