java中的计时器问题使用超时异常时,代码不';超时异常后无法正确运行
我正在用java设计一个类似棋盘游戏go的游戏。在实现中,还有一种称为速度模式的模式,如果一名玩家没有在时间限制(5秒)内转弯,则另一名玩家将获胜。这种模式通常也可以在“捕获”对方棋子时获胜。满足上述任一条件后,将从主菜单再次运行游戏。在正常模式和速度模式下,当通过捕获满足win条件时,此功能可以正常工作。然而,当它在时间耗尽时获胜时,它的行为非常奇怪,几乎是随机地提示输入和打印 当时的代码如下:java中的计时器问题使用超时异常时,代码不';超时异常后无法正确运行,java,timer,exception-handling,timeoutexception,Java,Timer,Exception Handling,Timeoutexception,我正在用java设计一个类似棋盘游戏go的游戏。在实现中,还有一种称为速度模式的模式,如果一名玩家没有在时间限制(5秒)内转弯,则另一名玩家将获胜。这种模式通常也可以在“捕获”对方棋子时获胜。满足上述任一条件后,将从主菜单再次运行游戏。在正常模式和速度模式下,当通过捕获满足win条件时,此功能可以正常工作。然而,当它在时间耗尽时获胜时,它的行为非常奇怪,几乎是随机地提示输入和打印 当时的代码如下: public Boolean speedMode(Player player, P
public Boolean speedMode(Player player, Player opponent) {
ExecutorService service = Executors.newSingleThreadExecutor();
try {
Runnable r = new Runnable() {
Boolean outOfRange;
public void run() {
do {
outOfRange = takeTurn(player);
} while (outOfRange == true);
}
};
Future<?> f = service.submit(r);
f.get(5, TimeUnit.SECONDS);
} catch (final InterruptedException e) {
System.out.println("The thread was interrupted during sleep, wait or join");
} catch (final TimeoutException e) {
player.setWon(false);
System.out.println("\n" + player.getName() +", you took too long... ");
return true;
} catch (final ExecutionException e) {
System.out.println("An exception from within the Runnable task");
}
return false;
}
基本上我的问题是,;有人能告诉我为什么在抛出TimeoutException后启动新游戏不能正常工作吗
有什么东西我需要关闭,也许在定时器方法?它几乎就像它仍然在后台运行
else {
do {
timeOut = speedMode(second, first);
if(winCheck1(first) == true || timeOut == true){
break;
}
timeOut = speedMode(first, second);
} while (winCheck1(second) != true && timeOut != true);
if(player1.isWon() == true){
System.out.println("\n\nCongratulations " + player1.getName() + " you are the winner!\n\n");
}
else{
System.out.println("\n\nCongratulations " + player2.getName() + " you are the winner!\n\n");
}
}
//reload the menu
Game game = new Game();
}
如果不在执行器上调用shutdown,则执行器创建的工作线程将挂起。(这与声明执行器的作用域无关。)如果它是非守护进程线程,那么它将使旧JVM保持活动状态
您的任务还需要对中断做出响应。请参阅有关立即关机的文档:
除了尽最大努力尝试停止处理积极执行的任务之外,没有其他保证。例如,典型的实现将通过Thread.interrupt()取消,因此任何未能响应中断的任务可能永远不会终止
“响应中断”意味着检查Thread.currentThread().isInterrupted()
,查看线程是否已被取消,然后对其执行操作(查找停止位置、执行清理并退出运行方法),并恢复中断标志(Thread.currentThread().interrupt()
)如果捕捉到InterruptedException或InterruptedIOException
有什么东西我需要关闭,也许在定时器方法?它几乎就像它仍然在后台运行
else {
do {
timeOut = speedMode(second, first);
if(winCheck1(first) == true || timeOut == true){
break;
}
timeOut = speedMode(first, second);
} while (winCheck1(second) != true && timeOut != true);
if(player1.isWon() == true){
System.out.println("\n\nCongratulations " + player1.getName() + " you are the winner!\n\n");
}
else{
System.out.println("\n\nCongratulations " + player2.getName() + " you are the winner!\n\n");
}
}
//reload the menu
Game game = new Game();
}
如果不在执行器上调用shutdown,则执行器创建的工作线程将挂起。(这与声明执行器的作用域无关。)如果它是非守护进程线程,那么它将使旧JVM保持活动状态
您的任务还需要对中断做出响应。请参阅有关立即关机的文档:
除了尽最大努力尝试停止处理积极执行的任务之外,没有其他保证。例如,典型的实现将通过Thread.interrupt()取消,因此任何未能响应中断的任务可能永远不会终止
“响应中断”意味着检查
Thread.currentThread().isInterrupted()
,查看线程是否已被取消,然后对其执行操作(查找停止位置、执行清理并退出运行方法),并恢复中断标志(Thread.currentThread().interrupt()
)如果捕获到InterruptedException或InterruptedOException。谢谢!我有点困惑,但如何做到这一点,因为这是我第一次处理ExecutorService。我试着打电话给服务部;在抛出异常试图关闭它但它似乎不起作用后,关于我做错了什么有什么建议吗?@K.McDonald:不知道takeTurn方法在做什么,如果它不处理中断,那么关闭将不起作用,请参阅我的编辑。谢谢!我有点困惑,但如何做到这一点,因为这是我第一次处理ExecutorService。我试着打电话给服务部;在抛出异常试图关闭它但它似乎不起作用后,关于我做错了什么有什么建议吗?@K.McDonald:不知道takeTurn方法在做什么,如果它不处理中断,那么关闭将不起作用,请参阅我的编辑。