我想实现迭代加深(增量树构建)。这是我的代码的一部分,我会问:JAVA:如何在特定时间后停止函数的执行?
ExecutorService executorService = Executors.newSingleThreadExecutor();
Set<Callable<Integer>> callables = new HashSet<Callable<Integer>>();
callables.add(new Callable<Integer>() {
public Integer call() throws Exception {
iterativeDeepening(depthLimit, board);
return -1;
}
});
callables.add(new Callable<Integer>() {
public Integer call() throws Exception {
Thread.sleep(500);
return 1;
}
});
try{
executorService.invokeAny(callables, 1000, TimeUnit.MILLISECONDS);
}catch(TimeoutException | InterruptedException ex){
executorService.shutdown();
}
executorService.shutdown();
从我有时间阅读invokeAny()限制它应该结束,因为一旦达到限期执行的可调用对象。它在我长时间睡眠而不是我的函数iterativeDeepening(depthLimit,board)时起作用。如何使它与我的功能一起工作? 下面我的代码粘贴到该函数:
public void iterativeDeepening(byte depthLimit, byte[] board){
for(byte depth=1;depth<depthLimit;depth++){
GameTree gameTree= new GameTree();
byte[] tempBoard = new byte[14];
for(byte i=0;i<14;i++){
tempBoard[i] = board[i];
}
Node <byte[]> root= new Node<byte[]>(tempBoard, player);
try {
gameTree.buildGameTree(depth, root);
} catch (OutOfMemoryError E) {
gameTree.eraseGameTree(depth,root);
System.gc();
}
MiniMax minimax = new MiniMax(player);
move= minimax.selectMove(depth, root);
}
}
如果你知道一个更好的办法,使之或不知道如何成功地阻止我的函数的执行,请让我知道。我也尝试了一下本主题中提到的Runnable接口: How to stop execution after a certain time in Java? 但它的工作原理是一样的。
如果你希望它是自包含的,你可以在进入循环之前存储一个System.currentTimeMillis()变量。然后在循环的每次迭代中检查if(System.currentTimeMillis() - startTime> = maxRunTime)。 – Radiodef
这不是一个解决方案,因为在循环内部有一个函数gameTree.buildGameTree(深度,根);其本身有时需要比截止日期更重要的时间。 – user2923339
您可以将开始时间作为参数传递。 @AndreyChaschev可能是正确的,你最好的选择就是检查中断。中断一个线程不一定会过早结束它,而无需自己检查中断。 – Radiodef