强制终止停留在某些方法的API调用上的线程 [英] Forceful termination of a thread stuck on an API call of some method

查看:91
本文介绍了强制终止停留在某些方法的API调用上的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况如下:

我正在实现一个服务器,该服务器必须在每个请求的指定超时期限内超时或产生响应.因此,从服务器的角度确保了每个请求的响应(当然,由于传输层故障等原因,该响应可能不会到达客户端.)

I am implementing a server that must timeout or produce a response within the specified timeout period for each request. Thus each request is ensured a response from the server's point of view (the response, of course, might not reach the client due to transport layer failure, etc...).

为了实现上述语义,每个请求都产生一个线程(实际上是从线程池中检索一个可用的线程),并通过对同步对象的通知等待其响应.等待时间受到发送给对象的wait方法的超时参数的限制.产生的线程将请求委托给实际上知道如何处理请求的对象.该对象的调用API是已知的,但是没有已知的服务级别协议可以指定调用永远不会无限期挂起. (特别是在我的情况下,服务器实际上是CORBA客户端-但这不是重点.)

In order to implement the above semantics, each request spawns a thread (actually retrieves an available one from a thread pool) and awaits its response via a notify on a sync object. The wait period is limited with a timeout param sent to Object's wait method. The spawned thread delegates the request to an object that actually knows how to handle the request. This object's call API is known, but there is no known service level agreement which would specify that a call will never hang indefinitely. (Specifically in my case, the Server is actually a CORBA client - but that is not the point.)

现在,让我感兴趣的是,即使我当前在方法调用上被阻塞,我是否有办法以某种方式检测到该线程没有响应,然后杀死它(中断它)?

Now, what interests me, is whether or not there is a way for me to somehow detect that that thread is not responsive and then kill it (interrupt it) even though I am currently blocked on a method call?

顺便说一句,我知道我可以保留对线程对象的引用,并在预先指定的时间后调用它的interrupt()方法.不幸的是,这不能确保中断"语义.

By the way, I know that I can keep a reference to the thread object and after a pre-specified amount of time call its interrupt() method. That unfortunately does not ensure "interrupt" semantics...

Java线程原始弃用

谢谢

推荐答案

我认为,如果您不能依赖interrupt()语义,那么您可能会走运.没有可靠或安全的方法来强制终止线程.任何语言都是危险的.

I think if you can't rely on interrupt() semantics then you may be out of luck. There is no reliable or safe way to forcefully terminate a thread. It's dangerous in any language.

我希望interrupt()可以为您提供所需的内容,即使可以绕开它. CORBA调用应该通过标准的网络类,因此应该在任何阻塞的调用上正确地中断.

I would hope that interrupt() would get you what you need even if it could be circumvented. The CORBA calls should be going through the standard network classes and therefore should properly interrupt on any blocking calls.

除了产生完整的子进程之外,这是您所能做的最好的事情.

It's the best you can do short of spawning full child processes.

这篇关于强制终止停留在某些方法的API调用上的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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