开发者

(J)Ruby - Is killing a thread okay?

In Java, it's not okay to stop threads. So, I'm wondering whether this (Thread.exit) is okay in Ruby?

(FWIW, I'm using JRu开发者_运维技巧by flavour of Ruby)


In general, forcibly stopping or killing threads is always contraindicated, because you can't guarantee clean release of resources and locks that thread holds. So Nick is right, a variable indicating it's time for the thread to shut itself down is generally better.

For your case, however, there's probably no other option than to forcibly kill the thread. The safest route would be to Thread#raise something in the thread to allow normal exception-handling and ensure blocks to clean up resources. But of course that can be rescued by the user. However, JRuby should also run ensure blocks in response to an unrescuable Thread#kill. In either case, you can't guarantee that you're not interrupting an ensure block in progress, but for your case perhaps that's an acceptable risk.


I have no knowledge at all of Ruby - but I would still think it is probably safer to have a variable or flag to indicate when you would like the thread to end. You can then safely finish writing to memory and close resources that may have been opened.

N

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜