A new thread separate from the main thread's execution, can be created using Thread.new
.
thr = Thread.new {
sleep 1 # 1 second sleep of sub thread
puts "Whats the big deal"
}
This will automatically start the execution of the new thread.
To freeze execution of the main Thread, until the new thread stops, use join
:
thr.join #=> ... "Whats the big deal"
Note that the Thread may have already finished when you call join, in which case execution will continue normally. If a sub-thread is never joined, and the main thread completes, the sub-thread will not execute any remaining code.