超时java代码?

时间:2014-06-18 09:12:33

标签: java timeout online-compilation

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

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

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

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

任何帮助将不胜感激! 感谢

3 个答案:

答案 0 :(得分:3)

我做过简单的TimeoutThread' exe由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。如果你想要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
    }

答案 1 :(得分:2)

我想看看在Java中使用ExecutorService并让类具有你想要超时实现runnable的功能 - 所以使用Java的线程功能来帮助你。

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

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

但是您可能需要稍微检查一下文档以使其适用于您的用例。

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

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

答案 2 :(得分:1)

您可以使用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
    });

参考

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