Kill a thread and pass execution to another thread : kill « Threads « Ruby






Kill a thread and pass execution to another thread


t1 = Thread.new { sleep 100 }
t2 = Thread.new do
  if Thread.current == Thread.main
    puts "This is the main thread."   # Does NOT print
  end
  1.upto(1000)
    sleep 0.1
  end
end

Thread.kill(t1)          
Thread.pass(t2)          # Pass execution to t2 now
t3 = Thread.new do
  sleep 20
  Thread.exit            
  puts "Can't happen!"   
end
Thread.kill(t2)          
# Now exit the main thread (killing any others)
Thread.exit

 








Related examples in the same category