【发布时间】: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