Repository navigation
Expand file tree
/
Copy pathexecutor.rb
More file actions
86 lines (70 loc) · 1.95 KB
/
Copy pathexecutor.rb
File metadata and controls
86 lines (70 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# frozen_string_literal: true
module SolidQueue
module Java
# Worker pool implementation - uses Java ExecutorService on JRuby, Threads on TruffleRuby
class Executor
if RUBY_ENGINE == 'jruby'
java_import java.util.concurrent.Executors
java_import java.util.concurrent.TimeUnit
attr_reader :executor_service, :worker_count
def initialize(worker_count = 4)
@worker_count = worker_count
@executor_service = Executors.newFixedThreadPool(worker_count)
@futures = []
end
def submit(&block)
future = @executor_service.submit(block)
@futures << future
future
end
def shutdown
@executor_service.shutdown
@executor_service.awaitTermination(30, TimeUnit::SECONDS)
end
def shutdown_now
@executor_service.shutdownNow
end
def wait_for_all
@executor_service.awaitTermination(Integer::MAX_VALUE, TimeUnit::SECONDS)
end
else
# TruffleRuby: use native threads
attr_reader :worker_count, :threads
def executor_service
self
end
def initialize(worker_count = 4)
@worker_count = worker_count
@threads = []
@queue = ::Queue.new
@shutdown = false
worker_count.times do
@threads << Thread.new { run_worker }
end
end
def submit(&block)
@queue.push(block)
end
def shutdown
shutdown_now
@threads.each(&:join)
end
def shutdown_now
@shutdown = true
@queue.close
end
def wait_for_all
@threads.each(&:join)
end
private
def run_worker
while (work = @queue.pop)
work.call
end
rescue ClosedQueueError
# Queue closed, thread exits
end
end
end
end
end