基于Java中的某个事件终止程序执行的最佳技术是什么
我正在用Java设计一款基于卡片的游戏。我已经实现了游戏逻辑。它工作得很好。现在我想添加一个功能,可以检查玩家玩游戏的时间。如果时间超过阈值限制,我想终止游戏的执行。请告诉我实现此功能的最佳策略是什么?创建线程并检查时间是一种好的技术,还是有其他技术可以实现这一点 编辑:很抱歉描述不清楚。我想实现的是,无论玩家在执行序列中的什么位置,当时间限制达到时,程序应该终止。如果我在循环条件下执行检查,那么如果时间还剩,程序将继续并完成循环中的指令集,但是如果时间结束,即使程序进入执行循环,它也应该停止它正在做的任何事情。这就是我想要实现的 谢谢,基于Java中的某个事件终止程序执行的最佳技术是什么,java,Java,我正在用Java设计一款基于卡片的游戏。我已经实现了游戏逻辑。它工作得很好。现在我想添加一个功能,可以检查玩家玩游戏的时间。如果时间超过阈值限制,我想终止游戏的执行。请告诉我实现此功能的最佳策略是什么?创建线程并检查时间是一种好的技术,还是有其他技术可以实现这一点 编辑:很抱歉描述不清楚。我想实现的是,无论玩家在执行序列中的什么位置,当时间限制达到时,程序应该终止。如果我在循环条件下执行检查,那么如果时间还剩,程序将继续并完成循环中的指令集,但是如果时间结束,即使程序进入执行循环,它也应该停止它
塔拉·辛格(Tara Singh)你一开始是如何保持比赛的?如果,正如我所怀疑的那样,是某种循环,那么为什么不允许循环在给定的时间条件下终止呢?您首先是如何让游戏继续进行的?如果,正如我所怀疑的那样,是某种循环,那么为什么不允许循环在给定的时间条件下终止呢?有两种可能性:
我想在这里检查运行时间就足够了,但是如果你想使用线程,这里有一个简单的方法。您也可以使用
计时器
而不是创建自己的线程。想法是一样的<代码>TimerTask应在超时发生时中断主线程
class Main {
public static void main() throws Exception {
final long timeout_ms = TimeUnit.MINUTES.toMillis(60);
//Store the main thread ref. so the interruption task can use it
final Thread me = Thread.currentThread();
new Thread(){
@Override
public void run(){
// If the timeout happens, or this thread is interrupted
// due to VM termination etc.) interrupt the main thread.
try{
Thread.sleep(timeout_ms);
}catch(InterruptedException e){
//see finally block
}finally{
me.interrupt();
}
}
}.start();
// Executing the game in the main thread.
new Game().run();
}
}
然后
class Game implements Runnable {
@Override
public void run(){
// Basically, check for the interruption flag before you do
// something that takes time to execute.
while(!Thread.currentThread.isInterrupted()){
doSomething();
}
}
}
使用中断标志是解决此类问题的首选方法。使用中断标志而不是创建自己的信令标志,或者检查循环本身的运行时间的优点之一是,您可以利用其他API的中断支持
例如,您可以在游戏中使用Thread.sleep()
。如果不使用中断机制,则必须等待sleep()
返回。如果您确实使用了中断机制,sleep()
将立即返回,抛出InterruptedException
,这样您的应用程序就可以正常运行了。将更具响应性
每当你在应用程序中捕捉到InterruptedException
。除非您有特殊原因,否则请按以下方式处理:
try{
someMethod();
}catch(InterruptedException e){
//Restore interruption flag
Thread.currentThread.interrupt();
//If you have some clean up to do, do it here.
return;
}
每当应用程序启动时。抛出
InterruptedException
您必须“恢复”中断标志,以便在堆栈上中继中断消息,因为InterruptedException
将“清除”中断标志(为false)。我想检查此处和此处的运行时间将足够,但是如果您想使用线程,下面是一个简单的方法。您也可以使用计时器
而不是创建自己的线程。想法是一样的<代码>TimerTask应在超时发生时中断主线程
class Main {
public static void main() throws Exception {
final long timeout_ms = TimeUnit.MINUTES.toMillis(60);
//Store the main thread ref. so the interruption task can use it
final Thread me = Thread.currentThread();
new Thread(){
@Override
public void run(){
// If the timeout happens, or this thread is interrupted
// due to VM termination etc.) interrupt the main thread.
try{
Thread.sleep(timeout_ms);
}catch(InterruptedException e){
//see finally block
}finally{
me.interrupt();
}
}
}.start();
// Executing the game in the main thread.
new Game().run();
}
}
然后
class Game implements Runnable {
@Override
public void run(){
// Basically, check for the interruption flag before you do
// something that takes time to execute.
while(!Thread.currentThread.isInterrupted()){
doSomething();
}
}
}
使用中断标志是解决此类问题的首选方法。使用中断标志而不是创建自己的信令标志,或者检查循环本身的运行时间的优点之一是,您可以利用其他API的中断支持
例如,您可以在游戏中使用Thread.sleep()
。如果不使用中断机制,则必须等待sleep()
返回。如果您确实使用了中断机制,sleep()
将立即返回,抛出InterruptedException
,这样您的应用程序就可以正常运行了。将更具响应性
每当你在应用程序中捕捉到InterruptedException
。除非您有特殊原因,否则请按以下方式处理:
try{
someMethod();
}catch(InterruptedException e){
//Restore interruption flag
Thread.currentThread.interrupt();
//If you have some clean up to do, do it here.
return;
}
每当应用程序启动时。抛出
InterruptedException
您必须“还原”中断标志,以便在堆栈上中继中断消息,因为InterruptedException
将“清除”中断标志(为false)。这可能有助于详细说明此超时的目标。使用timertask是最好的方法。使用线程不会提供一致的行为。详细说明此超时的目标可能会有所帮助。使用timertask是最好的方法。使用线程不会提供一致的行为。