I have this code:
…
….
ping_thread = Thread.new {
loop do
sleep 2
ping
end
}
ping_thread.join
puts "TEST"
…
….
it executes the ping function but does not move on printing the “TEST” statement. The ping function has a simple “puts “PING”” statement inside of it. I want the ping_thread to run as a background thread.
Thread.newwill start your thread running in the background automatically, andThread#joinwill block until that thread finishes its job. So normally, joining the thread is the last thing you do, when the main execution thread is done doing its work.Try the following code and see if it does what you want:
Note that if you don’t join the thead in the end, it will die when the main execution thread completes its work, so that join is necessary.