为什么要取消Clojure期货继续使用CPU? [英] Why do cancelled Clojure futures continue using CPU?

查看:175
本文介绍了为什么要取消Clojure期货继续使用CPU?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多Java字节码的例子,我想从Clojure中执行。每个字节码序列可能包含一个无限循环,在这种情况下,我想停止运行它几秒钟后。我一直在寻找未来作为这样做的一种手段。我试过这两个代码:

I have many examples of Java bytecode, all of which I'd like to execute from Clojure. Each sequence of bytecode may contain an infinite loop, in which case I'd like to stop running it after a couple of seconds. I've been looking at futures as a means of doing this. Having hunted around for a couple of implementations I've tried both this code:

(deref (future (loop[a 1] (recur a)) :done!) 1000 :impatient!)

... https://gist.github.com/3124000

在这两种情况下,循环似乎已适当超时(在后一种情况下,未来报告已完成和取消),但我看到我的CPU使用率上升到99%左右,并留在那里。我也看到,每次我运行这个代码,我的Java进程得到一个额外的线程。

In both cases, the loop appears to be timed out appropriately (and in the latter case the future is reported to have been both done and cancelled), but I see my CPU usage rise to 99% or thereabouts and stay there. I also see that each time I run this code, my Java process gains an extra thread.

它看起来像我的未来正在取消,但代码仍然运行。在我的程序中,我需要运行,并且超时,一些非常紧的无限循环(例如,相当于20 PRINT GOTO 10的Java字节码),我没有修改我运行的代码的选项。

It looks to me like the future is being cancelled, but the code is still running. In my program I will need to run, and time out, some very tight infinite loops (e.g. the Java bytecode equivalent of "20 PRINT GOTO 10") and I don't have the option of modifying the code that I'm running.

任何想法为什么我看到这种行为;我可以做些什么来防止它;或者替代方法来实现我的目标,运行和超时这样的代码?

Any ideas why I'm seeing this behaviour; what I could do to prevent it; or alternative methods for me to realise my aim of running and timing out such code?

推荐答案

实际杀死在一个线程中执行的代码是使用已弃用的 .stop 方法。在很多情况下,它被弃用的原因并不重要。

The only way I've found to actual kill a the code executing inside a thread is to use the deprecated .stop method. In a lot of cases, the reason that it is deprecated isn't really important.

我在 clojail 。随意取出功能或只是拉入图书馆。

I had a function in clojail for doing just that. Feel free to snatch the function out or just pull in the library.

这篇关于为什么要取消Clojure期货继续使用CPU?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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