简体   繁体   English

在时间限制后停止/中断线程

[英]Stopping/Interrupting a thread after time limit

I have an application which uses a third party jar to do some task. 我有一个使用第三方jar执行某些任务的应用程序。 My application uses Spring Executor pool to spawn the threads. 我的应用程序使用Spring Executor池生成线程。

Question: I need to stop the current execution of a thread if the executing time exceeds the time limit 问题:如果执行时间超过时间限制,我需要停止当前执行线程

What I did: I googled a lot about interrupting thread. 我做了什么:我在Google上搜索了很多有关中断线程的信息。 Most of says to check the interrupt flag whether the thread is interrupted or not and if interrupted throw an interrupted exception. 多数人说检查中断标志是否线程被中断,如果被中断则抛出被中断的异常。 But in thread I'm calling a method which is in a third party jar which do most of the stuffs so I can't go there and change the code for checking the interrupt flag. 但是在线程中,我正在调用一个位于第三方jar中的方法,该方法可以处理大多数工作,因此我无法去那里更改用于检查中断标志的代码。

Consider the following code snippet: 考虑以下代码片段:

public void run() {
     boolean isDone = false;
     isDone = callThirdPartyMethod(); // here for some inputs thread takes more time (even never return for an hour!!) to process which I don't want and I need to stop/interrupt the thread
}

Approaches I follow for stopping/interrupting it 我遵循的停止/中断方法

  1. I had wrote a TimerTask which checks for the thread which is exceeding the time limit and call the interrupt() on them ( but it seems nothing happens as they continue their execution ). 我写了一个TimerTask来检查线程是否超过了时间限制,并在它们上面调用了interrupt()( 但是在他们继续执行时似乎什么也没发生 )。
  2. I changed the code of my TimerTask from interrupting it to calling the stop() on thread. 我将TimerTask的代码从中断改为在线程上调用stop()。 This works but it throws ThreadDeath error. 这可行,但是会引发ThreadDeath错误。 Also what I read in most of the blogs and even in stackoverflow is that we should never call the stop method. 我在大多数博客中甚至在stackoverflow中都读到的是,我们永远不要调用stop方法。

Question: I need to stop the current execution of a thread if the executing time exceeds the time limit 问题:如果执行时间超过时间限制,我需要停止当前执行线程

There is no easy answer here. 这里没有简单的答案。 Typically you create one thread to make the third party library call and one thread to watch the clock and kill the other thread when a timer expires. 通常,您创建一个线程来进行第三方库调用,并创建一个线程来监视时钟并在计时器到期时杀死另一个线程。

You can call interrupt the third party library thread but as you mention there is no guarantee that callThirdPartyMethod() will listen for the interrupt. 您可以调用第三方库线程的中断,但是正如您提到的,并不能保证callThirdPartyMethod()将侦听该中断。 You can call stop() but this is deprecated for good reasons . 您可以调用stop()出于充分原因不建议使用此方法。

The only addition mechanism is to close something that the third party library is using. 唯一的添加机制是关闭第三方库正在使用的内容。 Maybe a socket or other connection could be closes out from under the library. 也许套接字或其他连接可能从库下面被关闭了。

If there is not way to do it then thread.stop() is your only (unfortunate) alternative unless you can get at the source of the library. 如果没有办法,除非您可以从库的源头获得,否则thread.stop()是您唯一(不幸的)选择。

Otherwise you are SOL unfortunately. 否则,很遗憾,您是SOL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM