-
Notifications
You must be signed in to change notification settings - Fork 0
/
2020-threads.rb
43 lines (35 loc) · 976 Bytes
/
2020-threads.rb
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
# We have a list tasks.
# Each task takes a random amount of time to complete.
# We want to work on a few tasks at once to speed things up.
# But not work on all taks at once to avoid abusing resources.
def in_parallel(tasks, max_threads: 10, &block)
thread_count = 0
while !tasks.empty?
puts "Checking for available theards"
if thread_count >= max_threads
puts "Going to sleep"
sleep(0.1)
puts "Woke up"
next
end
thread_count += 1
puts "Start new thread. thread_count: #{thread_count}"
Thread.new(tasks.shift) do |task|
yield(task)
thread_count -= 1
puts "Thread finished. thread_count: #{thread_count}"
end
end
puts "All done"
puts "Threads used: #{Thread.list.size}"
end
def work_on(task)
puts "Work on #{task} ⏱"
# Simulate random delay
sleep(rand(1..10).to_f / 10)
puts "Work on #{task} ✅"
end
tasks = (1..30).to_a
in_parallel(tasks) do |task|
work_on(task)
end