Theads
Because of GIL, threads in ruby are not very useful for CPU-bound tasks. But they can still be used for IO-bound operations.
thread = Thread.new do
sleep 1
puts "Hello"
end
thread.join
Concurrent IO
require 'net/http'
require 'uri'
urls = [
"https://developer.mozilla.org/",
"https://elixir-lang.org/",
]
threads = urls.map do |url|
Thread.new do
response = Net::HTTP.get_response(URI.parse(url))
puts "#{response.code} #{url}"
end
end
threads.each(&:join)
Threads are hard. There are tools like Mutex and Queue in the
thread module to achieve synchronization and data sharing. But you
don't have to work with threads. There are better options.