使用超时异常时 java 中的计时器问题,代码在超时异常后 运行 不正确
Timer issues in java when using Timeout Exceptions, code doesn't run correctly after a Timeout Exception
我正在 java 设计一款类似于棋盘游戏的游戏。在实现中还有一种称为速度模式的模式,如果玩家没有在时间限制(5 秒)内转弯,则另一位玩家获胜。这种模式也可以正常赢得 "capturing" 对手棋子。满足这些条件之一后,游戏将再次从主菜单 运行。当通过捕获满足获胜条件时,这在正常模式和速度模式下工作正常。当它在时间 运行 之前获胜时,它表现得非常奇怪,几乎 运行domly 提示输入和打印。
时间代码如下:
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();
}
基本上我的问题是;谁能告诉我为什么在抛出 TimeoutException 后无法正常开始新游戏?
Is there something I need to close perhaps in the timer method?It's almost like it's still running in the back ground.
如果您不对执行程序调用关闭,则执行程序创建的工作线程将挂起。 (这与您声明执行程序的范围无关。)如果它是非守护线程,那么它将使旧的 JVM 保持活动状态。
此外,您的任务需要响应中断。请参阅 shutdownNow 的文档:
There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread.interrupt(), so any task that fails to respond to interrupts may never terminate.
"Responding to interrupts"表示检查Thread.currentThread().isInterrupted()
线程是否已经被取消,然后对其进行操作(寻找停止位置,做清理,退出运行方法),如果捕获到 InterruptedException 或 InterruptedIOException,则恢复中断标志 (Thread.currentThread().interrupt()
)。
我正在 java 设计一款类似于棋盘游戏的游戏。在实现中还有一种称为速度模式的模式,如果玩家没有在时间限制(5 秒)内转弯,则另一位玩家获胜。这种模式也可以正常赢得 "capturing" 对手棋子。满足这些条件之一后,游戏将再次从主菜单 运行。当通过捕获满足获胜条件时,这在正常模式和速度模式下工作正常。当它在时间 运行 之前获胜时,它表现得非常奇怪,几乎 运行domly 提示输入和打印。
时间代码如下:
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();
}
基本上我的问题是;谁能告诉我为什么在抛出 TimeoutException 后无法正常开始新游戏?
Is there something I need to close perhaps in the timer method?It's almost like it's still running in the back ground.
如果您不对执行程序调用关闭,则执行程序创建的工作线程将挂起。 (这与您声明执行程序的范围无关。)如果它是非守护线程,那么它将使旧的 JVM 保持活动状态。
此外,您的任务需要响应中断。请参阅 shutdownNow 的文档:
There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread.interrupt(), so any task that fails to respond to interrupts may never terminate.
"Responding to interrupts"表示检查Thread.currentThread().isInterrupted()
线程是否已经被取消,然后对其进行操作(寻找停止位置,做清理,退出运行方法),如果捕获到 InterruptedException 或 InterruptedIOException,则恢复中断标志 (Thread.currentThread().interrupt()
)。