Try to interrupt a thread : raise « Threads « Ruby






Try to interrupt a thread


factorial1000 = Thread.new do
  begin
    prod = 1
    1.upto(1000) {|n| prod *= n }
    puts "1000! = #{prod}"
  rescue
    # Do nothing...
  end
end


if factorial1000.alive?
  factorial1000.raise("Stop!")
  puts "Calculation was interrupted!"
else
  puts "Calculation was successful."
end

 








Related examples in the same category