Skip to content

Commit 76d6914

Browse files
committed
Created stubs for RubyThreadPoolExecutor.
1 parent b0a1efd commit 76d6914

File tree

3 files changed

+185
-4
lines changed

3 files changed

+185
-4
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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

lib/concurrent/thread_pool_executor.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#require 'concurrent/ruby_thread_pool_executor'
1+
require 'concurrent/ruby_thread_pool_executor'
22

33
module Concurrent
44

@@ -23,8 +23,8 @@ module Concurrent
2323
class ThreadPoolExecutor < JavaThreadPoolExecutor
2424
end
2525
else
26-
## @!macro thread_pool_executor
27-
#class ThreadPoolExecutor < RubyThreadPoolExecutor
28-
#end
26+
# @!macro thread_pool_executor
27+
class ThreadPoolExecutor < RubyThreadPoolExecutor
28+
end
2929
end
3030
end
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require 'spec_helper'
2+
require_relative 'cached_thread_pool_shared'
3+
require_relative 'fixed_thread_pool_shared'
4+
5+
module Concurrent
6+
7+
describe RubyThreadPoolExecutor do
8+
9+
after(:each) do
10+
subject.kill
11+
sleep(0.1)
12+
end
13+
14+
context 'cached thread pool emulation' do
15+
16+
let(:described_class) do
17+
Class.new(RubyThreadPoolExecutor) do
18+
def initialize(opts = {})
19+
idletime = (opts[:thread_idletime] || opts[:idletime] || 60).to_i
20+
max_length = opts.fetch(:max_threads, RubyThreadPoolExecutor::DEFAULT_MAX_POOL_SIZE).to_i
21+
raise ArgumentError.new('idletime must be greater than zero') if idletime <= 0
22+
raise ArgumentError.new('max_threads must be greater than zero') if max_length <= 0
23+
super(opts)
24+
end
25+
end
26+
end
27+
28+
subject { described_class.new(max_threads: 5) }
29+
30+
it_should_behave_like :cached_thread_pool
31+
end
32+
33+
context 'fixed thread pool emulation' do
34+
35+
let(:described_class) do
36+
Class.new(RubyThreadPoolExecutor) do
37+
def initialize(max_threads)
38+
super(min_threads: max_threads, max_threads: max_threads, idletime: 0)
39+
end
40+
end
41+
end
42+
43+
subject { described_class.new(max_threads: 5) }
44+
45+
it_should_behave_like :fixed_thread_pool
46+
end
47+
end
48+
end

0 commit comments

Comments
 (0)