java 线程不断循环处理,Lua / Java / LuaJ-处理或中断无限循环和线程

I'm using LuaJ to run user-created Lua scripts in Java. However, running a Lua script that never returns causes the Java thread to freeze. This also renders the thread uninterruptible. I run the Lua script with:

JsePlatform.standardGlobals().loadFile("badscript.lua").call();

badscript.lua contains while true do end.

I'd like to be able to automatically terminate scripts which are stuck in unyielding loops and also allow users to manually terminate their Lua scripts while they are running. I've read about debug.sethook and pcall, though I'm not sure how I'd properly use them for my purposes. I've also heard that sandboxing is a better alternative, though that's a bit out of my reach.

This question might also be extended to Java threads alone. I've not found any definitive information on interrupting Java threads stuck in a while (true);.

The online Lua demo was very promising, but it seems the detection and termination of "bad" scripts is done in the CGI script and not Lua. Would I be able to use Java to call a CGI script which in turn calls the Lua script? I'm not sure that would allow users to manually terminate their scripts, though. I lost the link for the Lua demo source code but I have it on hand. This is the magic line:

tee -a $LOG | (ulimit -t 1 ; $LUA demo.lua 2>&1 | head -c 8k)

Can someone point me in the right direction?

Some sources:

解决方案

I struggled with the same issue and after some digging through the debug library's implementation, I created a solution similar to the one proposed by David Lewis, but did so by providing my own DebugLibrary:

package org.luaj.vm2.lib;

import org.luaj.vm2.LuaValue;

import org.luaj.vm2.Varargs;

public class CustomDebugLib extends DebugLib {

public boolean interrupted = false;

@Override

public void onInstruction(int pc, Varargs v, int top) {

if (interrupted) {

throw new ScriptInterruptException();

}

super.onInstruction(pc, v, top);

}

public static class ScriptInterruptException extends RuntimeException {}

}

Just execute your script from inside a new thread and set interrupted to true to stop the execution. The exception will be encapsulated as the cause of a LuaError when thrown.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值