是否可以使用计时器来杀死后台线程中的流氓方法?
我有一段代码调用来自第三方库 regexKitLite 的方法(即 processRegex),该方法对一堆数据运行正则表达式匹配。我使用 PerformSelectorInBackground: 在 bg 线程中运行此方法。我发现在某些情况下,如果数据无效或损坏,则 processRegex 方法会锁定线程并陷入递归循环,这可能需要永远回来,这是我想避免的。
我正在考虑使用一个基本上等待 x 时间的计时器,如果在指定的时间过去之前它没有失效,那么它会杀死正在运行该方法的 bg 线程。
然而我的问题是:
我无法在与正则表达式方法相同的线程中启动计时器,因为该线程在尝试处理数据时可能会锁定,并且不会像我下面尝试的那样调用我的计时器。
// setup timer to restrict the amount of time a regex can take before killing it
myTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(resultProcessingTimedOut:) userInfo:nil repeats:NO];
resultsArray = [NSMutableArray arrayWithArray:[html arrayOfCaptureComponentsMatchedByRegex:]; // <-- could lock up thread!!
[myTimer invalidate];
其次,根据苹果文档,您必须在启动计时器的同一线程中使计时器无效,并且我无法在其自己的后台线程中启动计时器,因为我不确定如何使它无效?
最后,我如何杀死正在运行正则表达式的线程,因为您不应该终止线程。
关于如何处理这种情况有什么想法吗?
谢谢
I have a piece of code that calls a method (i.e. processRegex) which is from a 3rd party library, regexKitLite, that runs a regex match on a bunch of data. I am running this method in a bg thread using performSelectorInBackground:. I have found that in some cases if the data is invalid or corrupted then the processRegex method locks up the thread and gets caught in a recursive loop which could take forever to come back which i obv would like to avoid.
I was thinking of using a timer that basically waits x amount of time and if it hasnt been invalidated before the specified time passes it then kills the bg thread that is running the method.
My problem however is:
I cant launch the timer in the same thread as the regex method because that thread could lock up while trying to process the data and wouldnt call my timer as i tried below.
// setup timer to restrict the amount of time a regex can take before killing it
myTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(resultProcessingTimedOut:) userInfo:nil repeats:NO];
resultsArray = [NSMutableArray arrayWithArray:[html arrayOfCaptureComponentsMatchedByRegex:]; // <-- could lock up thread!!
[myTimer invalidate];
Secondly, according to the apple docs you have to invalidate the timer in the same thread that started it and i cant launch the timer in its own background thread because im not sure how i would then invalidate it?
Lastly, how would i even kill the thread that is running the regex as you are not supposed to terminate threads.
Any ideas on how to deal with this situation???
thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简而言之,不,你不能任意杀死另一个线程。没有办法知道它被杀死时会处于什么状态,因此,内存泄漏和/或损坏必然会发生。
您可以通过以下几种方法之一解决此问题(包括组合):
修改 regexkit 以具有某种“停止”标志,可以在计时器在另一个线程中触发后设置该标志。定期检查它并在需要时清理/停止。
验证或限制输入正则表达式和输入数据以确保不会发生慢/长情况
In short, no, you cannot kill another thread arbitrarily. There is no way to know what state it'll be in when killed and, thus, the memory leaks and/or corruption are bound to happen.
You can solve this in one of several ways (including combining):
modify the regexkit to have some kind of "stop" flag that can be set after the timer fires in another thread. Periodically check it and clean up / stop when needed.
validate or limit the input regular expression and input data to ensure that the slow/long situation doesn't happen
break up the input data such that you can batch process it.