【问题标题】:externally ending infinite loop java外部结束无限循环java
【发布时间】:2015-09-05 23:39:17
【问题描述】:

我正在编写一个程序来标记一组学生提交的算法。我打算将他们的算法方法复制到程序中并运行它来查看结果;但要求算法运行时间不超过 10 秒。

我构造了一个 ExecutorService 来结束适用于 userInput 算法(已注释掉)但不适用于无限循环的算法。

根据我对线程的了解,中断它需要更改算法(添加一个标志),并且停止线程已被贬低,那么有没有其他方法可以在不更改算法的情况下结束无限循环?

代码如下:

public class TestAlgo{
private String move;

public static void main(String[] args){
    TestAlgo testAlgo = new TestAlgo();
    testAlgo.runGame();
}

public void runGame(){
    Cram game = new Cram();
    boolean start = game.startGame();


    while (start){
        ExecutorService executor = Executors.newSingleThreadExecutor();/////
        Future<String> future = executor.submit(new Task());/////
        move = "";
        try {
            System.out.println("Started..");
            move = future.get(10, TimeUnit.SECONDS);
            System.out.println("Finished!");
        } catch (TimeoutException e) {
            future.cancel(true);
            move = "Timeout";
            System.out.println("Terminated!");
        } catch (InterruptedException ie){
            System.out.println("Error: InterruptedException");
        } catch (ExecutionException ee){
            System.out.println("Error: ExecutionException");
        }
        System.out.println("Move: " + move);
        executor.shutdownNow();



        if (game.sendMove(move)) break;
        game.printBoard();
        if (game.getMove()) break;
        game.printBoard();
    }
}

// public static String algorithm(){
//     while (true){ //infinite loop
//         System.out.println("Algo is running...");
//     }
//     return "test";
// }

public static String algorithm(){
    Scanner userInputScanner = new Scanner(System.in);
    System.out.print("Please enter your move: ");
    String input = userInputScanner.nextLine();
    return input;
}}

class Task implements Callable<String> {
@Override
public String call() throws Exception {
    String move = TestAlgo.algorithm();
    return move;
}}

【问题讨论】:

  • 离题但if (game.sendMove(move) == true) 与更易读的if (game.sendMove(move)) 相同
  • 您可以结束整个程序,使用守护线程或System.exit()。这样就够了吗?

标签: java multithreading external interrupt


【解决方案1】:

Google Guava's SimpleTimeLimiter 应该会有所帮助。只需将ExecutorService 包裹在SimpleTimeLimiter 中,然后使用callWithTimeout 方法指定给定的超时时间;处理UncheckedTimeoutException 以指示已达到超时。最后,调用包裹在SimpleTimeLimiter中的ExecutorServiceshutdown方法。

【讨论】:

    【解决方案2】:

    作为在 JVM 的线程中运行不受信任的代码的替代方法,请考虑使用 ProcessBuilder 启动一个单独的进程。然后您可以使用 Process destroyForcibly 方法来终止它。

    【讨论】:

    • 谢谢你的建议,我去看看。
    【解决方案3】:

    您可以在while 循环中完成您的线程:

    public static String algorithm(){
        long start = System.currentTimeMillis();
        long end = start + 10*1000; // 10 seconds?
        while (System.currentTimeMillis() < end){ //not-so-infinite loop
             System.out.println("Algo is running...");
        }
        return "test";
    }
    

    但是如果你不想修改algorithm()方法,你可以检查this,或者this

    【讨论】:

    • 感谢您的建议,我会检查一下。
    猜你喜欢
    • 2013-09-30
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    相关资源
    最近更新 更多