Ruby Language Thread Basic Thread Semantics

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

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.



Got any Ruby Language Question?