看来,在Ruby2.4和2.5中,线程在调用#kill时不会立即死亡。此代码段将打印几次不死:
thread = Thread.new { loop {} }
thread.kill
puts "Not dead" while thread.alive?我想阻止主线程的执行,直到二级线程被杀死。我尝试使用thread.join.kill,但这当然会阻塞主线程,因为线程的循环永远不会终止。
如何确保线程在主线程继续运行之前被终止?
发布于 2018-03-26 11:25:30
找到了它;您仍然可以在杀死线程之后对它进行#join,所以您可以使用thread.kill.join来阻塞直到线程死掉。
这段代码从不打印不死。
thread = Thread.new { loop {} }
thread.kill.join
puts "Not dead" while thread.alive?发布于 2019-01-08 12:51:34
我要这么做:
thread = Thread.new { loop {} }
thread.kill
sleep 0.001 while thread.alive?这就是我如何在我的ThreadPool中终止线程。
https://stackoverflow.com/questions/49490278
复制相似问题