超时java代码?

我正在编写一个在线java编程应用程序,我将java代码作为用户输入,并在编译和执行后通过python脚本返回输出。

为了控制内存堆,我在JVM中运行代码时使用了-Xms和-Xmx的标准解决方案。 我已经安装了Sun Java 1.7.0_40。

现在问题是我对如何限制具有时间限制的代码感到困惑。 例如,用户在我的应用程序中提交的任何代码的运行时间不应超过T秒,其中T是一个变量。

我使用Timer类编写了一个简单的hack但问题是我必须使用大量的正则表达式将它注入用户代码中,我主要想避免它。 由于我比python和c ++更适合java作为程序员,我需要一些关于是否存在这种问题的简单解决方案或使用Timer类的优缺点的指导。

任何帮助都感激不尽! 谢谢

我已经使用ExecutorService完成了简单的’TimeoutThread’工具。

2class:

package eu.wordnice.thread; /*** Runa.java ***/ import java.util.concurrent.Callable; public class Runa implements Callable { 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> 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 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。 如果你想要Timeout Tread,只需将代码/调用放入Thread。

 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 ,并让具有您想要超时的function的类实现runnable – 所以使用Java的线程function来帮助您。

您应该能够使用以下代码超时线程:

 ExecutorService executor = Executors.newSingleThreadExecutor(); executor.invokeAll(Arrays.asList(new Task()), 10, TimeUnit.MINUTES); // Timeout of 10 minutes. executor.shutdown(); 

但是您可能需要查看一些文档以使其适用于您的用例。

有关此类事项的更多建议,请参阅以下post 。

不确定你是否想要在你的python代码或Java中使用超时,但希望这会有所帮助。

您可以使用ThreadPoolExecutor

样品:

 int corePoolSize = 5; int maxPoolSize = 10; long keepAliveTime = 5000; ExecutorService threadPoolExecutor = new ThreadPoolExecutor( corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.MILLISECONDS, new LinkedBlockingQueue() ); threadPoolExecutor.execute(new Runnable(){ @Override public void run() { // execution statements }); 

参考

  1. http://tutorials.jenkov.com/java-util-concurrent/threadpoolexecutor.html