加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

为什么退出Ruby线程会杀死我的整个程序?

发布时间:2020-12-16 23:28:05 所属栏目:百科 来源:网络整理
导读:我有这段代码: puts "Start"loop do Thread.start do puts "Hello from thread" exit end text = gets puts "#{text}"endputs "Done" 我期望看到“开始”后跟“线程中的Hello”,然后我可以输入回复给我的输入.相反,我得到“开始”和“来自线程的Hello”,然
我有这段代码:
puts "Start"
loop do
    Thread.start do
        puts "Hello from thread"
        exit
    end
    text = gets
    puts "#{text}"
end
puts "Done"

我期望看到“开始”后跟“线程中的Hello”,然后我可以输入回复给我的输入.相反,我得到“开始”和“来自线程的Hello”,然后程序退出.

从退出文档:

Terminates thr and schedules another thread to be run. If this thread
is already marked to be killed,exit returns the Thread. If this is
the main thread,or the last thread,exits the process.

但我以为我催生了一个新线程?为什么退出我的主要流程?

解决方法

您正在查看 Thread#exit文档. kill是 Kernel#exit,它终止了Ruby脚本.
puts "Start"
loop do
    Thread.start do
        puts "Hello from thread"
        Thread.exit
    end
    text = gets
    puts "#{text}"
end
puts "Done"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读