|
| 1 | +module Concurrent |
| 2 | + |
| 3 | + # @!macro thread_pool_executor |
| 4 | + class RubyThreadPoolExecutor |
| 5 | + |
| 6 | + # The maximum number of threads that will be created in the pool |
| 7 | + # (unless overridden during construction). |
| 8 | + DEFAULT_MAX_POOL_SIZE = 2**15 # 32768 |
| 9 | + |
| 10 | + # The minimum number of threads that will be created in the pool |
| 11 | + # (unless overridden during construction). |
| 12 | + DEFAULT_MIN_POOL_SIZE = 0 |
| 13 | + |
| 14 | + DEFAULT_MAX_QUEUE_SIZE = 0 |
| 15 | + |
| 16 | + # The maximum number of seconds a thread in the pool may remain idle before |
| 17 | + # being reclaimed (unless overridden during construction). |
| 18 | + DEFAULT_THREAD_IDLETIMEOUT = 60 |
| 19 | + |
| 20 | + OVERFLOW_POLICIES = [:abort, :discard, :caller_runs] |
| 21 | + |
| 22 | + # The maximum number of threads that may be created in the pool. |
| 23 | + attr_reader :max_length |
| 24 | + |
| 25 | + attr_reader :max_queue |
| 26 | + |
| 27 | + # Create a new thread pool. |
| 28 | + # |
| 29 | + # @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html |
| 30 | + def initialize(opts = {}) |
| 31 | + min_length = opts.fetch(:min_threads, DEFAULT_MIN_POOL_SIZE).to_i |
| 32 | + @max_length = opts.fetch(:max_threads, DEFAULT_MAX_POOL_SIZE).to_i |
| 33 | + idletime = opts.fetch(:idletime, DEFAULT_THREAD_IDLETIMEOUT).to_i |
| 34 | + @max_queue = opts.fetch(:max_queue, DEFAULT_MAX_QUEUE_SIZE).to_i |
| 35 | + overflow_policy = opts.fetch(:overflow_policy, :abort) |
| 36 | + |
| 37 | + raise ArgumentError.new('max_threads must be greater than zero') if @max_length <= 0 |
| 38 | + raise ArgumentError.new("#{overflow_policy} is not a valid overflow policy") unless OVERFLOW_POLICIES.include?(overflow_policy) |
| 39 | + end |
| 40 | + |
| 41 | + def min_length |
| 42 | + end |
| 43 | + |
| 44 | + def max_length |
| 45 | + end |
| 46 | + |
| 47 | + def length |
| 48 | + end |
| 49 | + alias_method :current_length, :length |
| 50 | + |
| 51 | + def largest_length |
| 52 | + end |
| 53 | + |
| 54 | + def scheduled_task_count |
| 55 | + end |
| 56 | + |
| 57 | + def completed_task_count |
| 58 | + end |
| 59 | + |
| 60 | + def idletime |
| 61 | + end |
| 62 | + |
| 63 | + def queue_length |
| 64 | + end |
| 65 | + |
| 66 | + def remaining_capacity |
| 67 | + end |
| 68 | + |
| 69 | + # Is the thread pool running? |
| 70 | + # |
| 71 | + # @return [Boolean] +true+ when running, +false+ when shutting down or shutdown |
| 72 | + def running? |
| 73 | + end |
| 74 | + |
| 75 | + # Is the thread pool shutdown? |
| 76 | + # |
| 77 | + # @return [Boolean] +true+ when shutdown, +false+ when shutting down or running |
| 78 | + def shutdown? |
| 79 | + end |
| 80 | + |
| 81 | + # Were all tasks completed before shutdown? |
| 82 | + # |
| 83 | + # @return [Boolean] +true+ if shutdown and all tasks completed else +false+ |
| 84 | + def terminated? |
| 85 | + end |
| 86 | + |
| 87 | + # Block until thread pool shutdown is complete or until +timeout+ seconds have |
| 88 | + # passed. |
| 89 | + # |
| 90 | + # @note Does not initiate shutdown or termination. Either +shutdown+ or +kill+ |
| 91 | + # must be called before this method (or on another thread). |
| 92 | + # |
| 93 | + # @param [Integer] timeout the maximum number of seconds to wait for shutdown to complete |
| 94 | + # |
| 95 | + # @return [Boolean] +true+ if shutdown complete or false on +timeout+ |
| 96 | + def wait_for_termination(timeout) |
| 97 | + end |
| 98 | + |
| 99 | + # Submit a task to the thread pool for asynchronous processing. |
| 100 | + # |
| 101 | + # @param [Array] args zero or more arguments to be passed to the task |
| 102 | + # |
| 103 | + # @yield the asynchronous task to perform |
| 104 | + # |
| 105 | + # @return [Boolean] +true+ if the task is queued, +false+ if the thread pool |
| 106 | + # is not running |
| 107 | + # |
| 108 | + # @raise [ArgumentError] if no task is given |
| 109 | + def post(*args) |
| 110 | + end |
| 111 | + |
| 112 | + # Submit a task to the thread pool for asynchronous processing. |
| 113 | + # |
| 114 | + # @param [Proc] task the asynchronous task to perform |
| 115 | + # |
| 116 | + # @return [self] returns itself |
| 117 | + def <<(task) |
| 118 | + end |
| 119 | + |
| 120 | + # Begin an orderly shutdown. Tasks already in the queue will be executed, |
| 121 | + # but no new tasks will be accepted. Has no additional effect if the |
| 122 | + # thread pool is not running. |
| 123 | + def shutdown |
| 124 | + end |
| 125 | + |
| 126 | + # Begin an immediate shutdown. In-progress tasks will be allowed to |
| 127 | + # complete but enqueued tasks will be dismissed and no new tasks |
| 128 | + # will be accepted. Has no additional effect if the thread pool is |
| 129 | + # not running. |
| 130 | + def kill |
| 131 | + end |
| 132 | + end |
| 133 | +end |
0 commit comments