Threads
Last updated
Hello from main thread!
Hello from thread!# Cria uma thread
thread = Thread.new do
puts "Hello from thread!"
end
puts "Hello from main thread!"
# Aguarda a thread terminar
thread.joindef handle(thread_id)
puts "Thread #{thread_id} is running..."
sleep(2) # Simula uma tarefa que leva 2 segundos
puts "Thread #{thread_id} is finished."
end
threads = []
# Criação das threads
3.times do |i|
threads << Thread.new(i + 1) do |thread_id|
handle(thread_id) # Passa o ID da thread como argumento
end
end
# Aguarda todas as threads finalizarem
threads.each_with_index do |thread, i|
thread.join
puts "Thread #{i + 1} has been finished."
end
puts "All threads are finished."Thread 1 is running...
Thread 2 is running...
Thread 3 is running...
Thread 1 is finished.
Thread 3 is finished.
Thread 2 is finished.
Thread 1 has been finished.
Thread 2 has been finished.
Thread 3 has been finished.
All threads are finished.