Skip to content

Commit 9a4fbcb

Browse files
committed
Create method ThreadPoolExecutor#available_worker_count to expose number of idle/uncreated worker threads
1 parent 9f40827 commit 9a4fbcb

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

lib/concurrent-ruby/concurrent/executor/fixed_thread_pool.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ module Concurrent
3939
# The number of tasks that have been completed by the pool since construction.
4040
# @return [Integer] The number of tasks that have been completed by the pool since construction.
4141

42+
# @!macro thread_pool_executor_attr_reader_available_worker_count
43+
# The number of worker threads that are available to process tasks (either idle or uncreated)
44+
# @return [Integer] The number of worker threads that are available to process tasks (either idle or uncreated)
45+
4246
# @!macro thread_pool_executor_attr_reader_idletime
4347
# The number of seconds that a thread may be idle before being reclaimed.
4448
# @return [Integer] The number of seconds that a thread may be idle before being reclaimed.

lib/concurrent-ruby/concurrent/executor/java_thread_pool_executor.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ def completed_task_count
7373
@executor.getCompletedTaskCount
7474
end
7575

76+
# @!macro thread_pool_executor_attr_reader_available_worker_count
77+
def available_worker_count
78+
@executor.getMaximumPoolSize - @executor.getActiveCount
79+
end
80+
7681
# @!macro thread_pool_executor_attr_reader_idletime
7782
def idletime
7883
@executor.getKeepAliveTime(java.util.concurrent.TimeUnit::SECONDS)

lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ def completed_task_count
6161
synchronize { @completed_task_count }
6262
end
6363

64+
# @!macro thread_pool_executor_attr_reader_available_worker_count
65+
def available_worker_count
66+
synchronize do
67+
uncreated_workers = @max_length - @pool.length
68+
idle_workers = @ready.length
69+
uncreated_workers + idle_workers
70+
end
71+
end
72+
6473
# @!macro executor_service_method_can_overflow_question
6574
def can_overflow?
6675
synchronize { ns_limited_queue? }

spec/concurrent/executor/thread_pool_executor_shared.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,27 @@
258258
end
259259
end
260260

261+
context '#available_worker_count' do
262+
subject do
263+
described_class.new(
264+
min_threads: 5,
265+
max_threads: 10,
266+
idletime: 60,
267+
max_queue: 0,
268+
fallback_policy: :discard
269+
)
270+
end
271+
272+
it 'returns the number of available (ready/idle or uncreated) workers' do
273+
expect(subject.available_worker_count).to eq 10
274+
latch = Concurrent::CountDownLatch.new(7)
275+
7.times{ subject.post{ sleep 0.1; latch.count_down } }
276+
expect(subject.available_worker_count).to eq 3
277+
expect(latch.wait(1)).to be_truthy
278+
expect(subject.available_worker_count).to eq 10
279+
end
280+
end
281+
261282
context '#fallback_policy' do
262283

263284
let!(:min_threads){ 1 }

0 commit comments

Comments
 (0)