File tree Expand file tree Collapse file tree 4 files changed +39
-0
lines changed
lib/concurrent-ruby/concurrent/executor Expand file tree Collapse file tree 4 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,10 @@ module Concurrent
39
39
# The number of tasks that have been completed by the pool since construction.
40
40
# @return [Integer] The number of tasks that have been completed by the pool since construction.
41
41
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
+
42
46
# @!macro thread_pool_executor_attr_reader_idletime
43
47
# The number of seconds that a thread may be idle before being reclaimed.
44
48
# @return [Integer] The number of seconds that a thread may be idle before being reclaimed.
Original file line number Diff line number Diff line change @@ -73,6 +73,11 @@ def completed_task_count
73
73
@executor . getCompletedTaskCount
74
74
end
75
75
76
+ # @!macro thread_pool_executor_attr_reader_available_worker_count
77
+ def available_worker_count
78
+ @executor . getMaximumPoolSize - @executor . getActiveCount
79
+ end
80
+
76
81
# @!macro thread_pool_executor_attr_reader_idletime
77
82
def idletime
78
83
@executor . getKeepAliveTime ( java . util . concurrent . TimeUnit ::SECONDS )
Original file line number Diff line number Diff line change @@ -61,6 +61,15 @@ def completed_task_count
61
61
synchronize { @completed_task_count }
62
62
end
63
63
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
+
64
73
# @!macro executor_service_method_can_overflow_question
65
74
def can_overflow?
66
75
synchronize { ns_limited_queue? }
Original file line number Diff line number Diff line change 258
258
end
259
259
end
260
260
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
+
261
282
context '#fallback_policy' do
262
283
263
284
let! ( :min_threads ) { 1 }
You can’t perform that action at this time.
0 commit comments