我正在编写一个在线java编程应用程序,其中我从用户获取java代码作为输入,并通过python脚本编译和执行后返回输出。
对于控制内存堆,我有一个标准的解决方案,即在JVM中运行代码时使用-Xms和-Xmx。我已经安装了Sun Java 1.7.0_40.现在的问题是我对如何用时间限制来限制代码感到困惑。例如,用户在我的应用程序中提交的任何代码都不应该运行超过T秒,其中T是一个变量。
我写了一个简单的hack使用定时器类,但问题是我必须使用大量的正则表达式注入它在用户代码,我主要想避免。作为一名程序员,我对python和c++比java更熟悉,因此我需要一些指导,了解是否存在一些简单的解决此类问题的方法,或者使用Timer类的优缺点。
任何帮助将非常感激!由于
我已经做了简单的'TimeoutThread',直到使用ExecutorService。
2类:
package eu.wordnice.thread;
/*** Runa.java ***/
import java.util.concurrent.Callable;
public class Runa implements Callable<Object> {
private Runnable run = null;
public Runa(Runnable run) {
this.run = run;
}
public Runa(Thread run) {
this.run = run;
}
public Runa() {}
public Object call() throws Exception {
if(run != null) {
run.run();
}
return -1;
};
}
:
package eu.wordnice.thread;
/*** TimeoutThread.java ***/
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
public class TimeoutThread {
public Runa run = null;
public ExecutorService executor = null;
public long timeout = 100L;
private boolean canceled = false;
private boolean runed = false;
public TimeoutThread(Runnable runit, long timeout) {
this(new Runa(runit), timeout);
}
public TimeoutThread(Runa runit, long timeout) {
this.run = runit;
if(timeout < 1L) {
timeout = 10L;
}
this.timeout = timeout;
}
public Object run() {
return this.run(false);
}
public Object run(Object defaulte) {
this.runed = true;
List<Future<Object>> list = null;
try {
this.executor = Executors.newCachedThreadPool();
list = executor.invokeAll(Arrays.asList(this.run), this.timeout, TimeUnit.MILLISECONDS);
} catch (Exception e) {
e.printStackTrace();
this.canceled = true;
}
executor.shutdown();
if(list == null) {
return defaulte;
}
if(list.size() != 1) {
return defaulte;
}
try {
Future<Object> f = list.get(0);
try {
return f.get();
} catch (Exception e) {
this.canceled = true;
}
} catch (Exception e) { }
return defaulte;
}
public boolean wasRunned() {
return this.runed;
}
public boolean wasCanceled() {
return this.canceled;
}
}
例子:
public static void main(String... blah) {
TimeoutThread thr = new TimeoutThread(new Runa() {
@Override
public Object call() throws Exception {
while(true) {
System.out.println("Yeeee");
Thread.sleep(300L);
}
}
}, 500L);
thr.run();
}
打印:
Yeeee
Yeeee
编辑!
对不起,这是Timeout Runnable。如果你想要超时时间,只需把代码/调用到线程。
public static void main(String... blah) {
final TimeoutThread thr = new TimeoutThread(new Runa() {
@Override
public Object call() throws Exception {
while(true) {
System.out.println("Yeeee");
Thread.sleep(300L);
}
}
}, 500L);
new Thread() {
@Override
public void run() {
thr.run(); //Call it
}
}.start(); //Run it
}
我会考虑在Java中使用ExecutorService来实现这一点,并让具有超时功能的类实现可运行-因此使用Java的线程功能来帮助您。
您应该能够使用以下代码使线程超时:
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.invokeAll(Arrays.asList(new Task()), 10, TimeUnit.MINUTES); // Timeout of 10 minutes.
executor.shutdown();
但是你可能需要检查一下文档,让它为你的用例工作。
关于这类事情的更多建议请看下面的文章。
不确定您是否想要在python代码或Java中使用超时,但希望这将有所帮助。
您可以使用ThreadPoolExecutor
int corePoolSize = 5;
int maxPoolSize = 10;
long keepAliveTime = 5000;
ExecutorService threadPoolExecutor =
new ThreadPoolExecutor(
corePoolSize,
maxPoolSize,
keepAliveTime,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()
);
threadPoolExecutor.execute(new Runnable(){
@Override
public void run() {
// execution statements
});
引用- http://tutorials.jenkov.com/java-util-concurrent/threadpoolexecutor.html