Skip to content

Commit

Permalink
Create method ThreadPoolExecutor#available_worker_count to expose num…
Browse files Browse the repository at this point in the history
…ber of idle/uncreated worker threads
  • Loading branch information
bensheldon authored and ioquatix committed Nov 14, 2023
1 parent 25ccddc commit d9bf21d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/concurrent-ruby/concurrent/executor/fixed_thread_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ module Concurrent
# The number of tasks that have been completed by the pool since construction.
# @return [Integer] The number of tasks that have been completed by the pool since construction.

# @!macro thread_pool_executor_attr_reader_available_worker_count
# The number of worker threads that are available to process tasks (either idle or uncreated)
# @return [Integer] The number of worker threads that are available to process tasks (either idle or uncreated)

# @!macro thread_pool_executor_attr_reader_idletime
# The number of seconds that a thread may be idle before being reclaimed.
# @return [Integer] The number of seconds that a thread may be idle before being reclaimed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ def completed_task_count
@executor.getCompletedTaskCount
end

# @!macro thread_pool_executor_attr_reader_available_worker_count
def available_worker_count
@executor.getMaximumPoolSize - @executor.getActiveCount
end

# @!macro thread_pool_executor_attr_reader_idletime
def idletime
@executor.getKeepAliveTime(java.util.concurrent.TimeUnit::SECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ def completed_task_count
synchronize { @completed_task_count }
end

# @!macro thread_pool_executor_attr_reader_available_worker_count
def available_worker_count
synchronize do
uncreated_workers = @max_length - @pool.length
idle_workers = @ready.length
uncreated_workers + idle_workers
end
end

# @!macro executor_service_method_can_overflow_question
def can_overflow?
synchronize { ns_limited_queue? }
Expand Down
21 changes: 21 additions & 0 deletions spec/concurrent/executor/thread_pool_executor_shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,27 @@
end
end

context '#available_worker_count' do
subject do
described_class.new(
min_threads: 5,
max_threads: 10,
idletime: 60,
max_queue: 0,
fallback_policy: :discard
)
end

it 'returns the number of available (ready/idle or uncreated) workers' do
expect(subject.available_worker_count).to eq 10
latch = Concurrent::CountDownLatch.new(7)
7.times{ subject.post{ sleep 0.1; latch.count_down } }
expect(subject.available_worker_count).to eq 3
expect(latch.wait(1)).to be_truthy
expect(subject.available_worker_count).to eq 10
end
end

context '#fallback_policy' do

let!(:min_threads){ 1 }
Expand Down

0 comments on commit d9bf21d

Please sign in to comment.