如何杀死不可中断的线程? [英] How to kill non-interruptable thread?

查看:141
本文介绍了如何杀死不可中断的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们举办 AI 编程比赛,参赛者将在<$ c $上运行 AI c> JVM 使用我们提供的 API 。我们通过使用 SecurityManager 限制它们可以做什么来将它们放入沙箱中,并且在运行时它们只是设置了几个标志,这是他们的决定。我们的系统和他们的 AI 之间的唯一交互是通过这些标志,所以如果他们的线程突然死亡,对我们没有任何不良影响。

We run an AI programming competition in which contestants will code an AI that runs on the JVM using our API we provide them. We put them into a sandbox by limiting what they can do with a SecurityManager, and during runtime they simply set several flags which are their decisions. The only interaction between our system and their AI is through these flags, so there are no bad effects on us if their thread were to suddenly die.

AI 计算得太长时,我们想关闭他们的线程。但是,我们无法找到保证我们将销毁其线程的方法。一个可能的原因是 AI 进入无限循环而没有阻塞,使得 Thread.interrupt()无用。 Thread.stop()是不可靠的,因为如果它们在try catch块中,将捕获 ThreadDeath 异常,并且对我们没有任何问题,因为他们没有触及任何坏事,我们不在乎他们是否会死。

When an AI computes far too long, we would like to shut down their thread. However, we can't find a way of guaranteeing that we will destroy their thread. One possible reason for this is that the AI goes into an infinite loop with no blocking, making Thread.interrupt() useless. Thread.stop() is unreliable since if they are in a try catch block the ThreadDeath exception will be caught, and has no issues for us because they don't touch anything bad and we don't care if they die.

目前我们只是忽略了他们的线程并在没有它们之后继续它们超时,但它们的无限循环将继续在后台处理,直到 JVM 死亡。这对我们来说是不可接受的,因为我们将在24/7的Web服务器上在后台运行匹配,因此我们希望尽可能多的稳定性。一种想法是在一个单独的 JVM 中运行每个游戏,但这比我们想要的要复杂得多。

Currently we just ignore their thread and continue on without them after they time out, but their infinite loop will continue processing in the background until the JVM dies. This is unacceptable to us because we will be running matches in the background on a web server 24/7, so we want as much stability as possible. One thought has been to run each game in a separate JVM, but that is far more complex than we would like to get.

有没有确定的消防方法来销毁线程?

Is there any sure fire way to destroy the thread?

推荐答案

在尝试了几件事后,我们得出结论没有保证的解决方案。通过在线程上调用stop(),该线程能够捕获ThreadDeath throwable并完全忽略它。因此,如果它在while循环中连续捕获它,或者如果它以递归方式调用捕获它的方法,则不能保证你可以杀死它。

After trying several things, we came to the conclusion that there is no guaranteed solution. By calling stop() on a thread, that thread is capable of catching the ThreadDeath throwable and ignoring it entirely. Thus, if it's in a while loop continuously catching it, or if it calls a method recursively that catches it, it is not guaranteed that you can kill it.

因为我们没有对在这种情况下运行的代码有任何控制,并且该代码不一定是Java(我们也支持Jython),我们可以提出的最佳解决方案是生成一个进入循环的线程在线程上连续调用suspend()然后stop()。结果适用于大多数情况,但偶尔无法杀死恶意线程。

Since we didn't have any control over the code that would be running in that case, and that code was not necessarily in Java (we were also supporting Jython), the best solution we could come up with was spawning a thread that went into a loop that continuously called suspend() and then stop() on the thread. The result worked for most cases, but occasionally was unable to kill a malicious thread.

这篇关于如何杀死不可中断的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆