如何销毁Java ScriptEngine实例?

时间:2022-09-25 21:09:17

I'm using Java 8. I'm not a java expert (haven't looked at java since college 10 years ago)

我正在使用Java 8.我不是java专家(10年前从未上过大学)

I create an instance of a ScriptEngine (Nashorn if it matters). I use it to eval several javascript files.

我创建了一个ScriptEngine的实例(Nashorn如果重要的话)。我用它来评估几个javascript文件。

The script chugs along quite happily in its own little javascript world. I love it.

该脚本在其自己的小javascript世界中非常开心。我喜欢它。

I also have a File System WatchService running, in case some of my java scripts get modified. When they get modified, I create a new ScriptEngine, and eval the new versions of the javascript files using the new ScriptEngine object.

我还运行了一个File System WatchService,以防我的一些java脚本被修改。当它们被修改后,我创建了一个新的ScriptEngine,并使用新的ScriptEngine对象评估新版本的javascript文件。

My problem is, I cant "kill" the old scriptEngine once I create the new one. The old script keeps on running indefinitely.

我的问题是,一旦我创建了新的scriptEngine,我就不能“杀死”旧脚本。旧脚本继续无限期运行。

I really want to free up the resources being used by the old scriptEngine object. AND I want it to stop doing the stuff its doing (since it could conflict with the new instance if they both try doing the same things).

我真的想释放旧scriptEngine对象使用的资源。我希望它停止做它所做的事情(因为它可能与新实例冲突,如果他们都尝试做同样的事情)。

So ... How do I completely eliminate the old ScriptEngine, including any threads spawned inside of it?

那么......我如何完全消除旧的ScriptEngine,包括在其中生成的任何线程?

1 个解决方案

#1


0  

Ok, the old problem with the leaking threads. Have you got a handle of the thread? Does something inherits Thread? Ok, then something like this:

好吧,泄漏线程的旧问题。你有线程的句柄吗?有些东西继承了Thread吗?好的,然后是这样的:

private void stopThread(Thread thread) {
    final Thread moribund = thread;
    if (moribund != null) {
        thread = null;
        moribund.interrupt();
    }
}

Maybe it helps.

也许有帮助。

#1


0  

Ok, the old problem with the leaking threads. Have you got a handle of the thread? Does something inherits Thread? Ok, then something like this:

好吧,泄漏线程的旧问题。你有线程的句柄吗?有些东西继承了Thread吗?好的,然后是这样的:

private void stopThread(Thread thread) {
    final Thread moribund = thread;
    if (moribund != null) {
        thread = null;
        moribund.interrupt();
    }
}

Maybe it helps.

也许有帮助。