I am designing a Card based Game in Java. I have implemented the game logic. It is working fine. Now I want to add a functionality which can check for the time player is playing the game. If the time goes above a threshold limit, I want to terminate the Execution of Game. Please suggest me what should be the best strategy to implement this feature? Is creating a Thread and checking the time a good technique or there is any other technique to achieve this?
Edit: Sorry for the vague description.What I wan开发者_JAVA技巧t to implement is, no matter where the player is in the execution sequence, when the time limit reaches, program should terminate. If i implement the check at the looping condition, then if the time is still left, program will continue and complete the set of instructions in the loop, but if the time is over even if the program is entered into the execution loop, it should stop doing whatever it is doing. This is what i want to implement.
Thanks, Tara Singh
Two possibilities:
- If you're using some sort of main loop where all the processing takes place, just add a check for the amount of time passed in there.
- You can create a Timer that runs when the time is up and executes some method that ends the game. The timer will take care of creating another thread and executing it for you. Have a look at: http://download.oracle.com/javase/6/docs/api/java/util/Timer.html
I guess checking the elapsed time here and there would be sufficient, but if you want to use threading, here is a simple way to do it. You can also use Timer
instead of creating your own thread. The idea is same; TimerTask
should interrupt the main thread when the timeout happens.
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();
}
}
and then
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();
}
}
}
Using the interruption flag is the preferred way to solve this kind of problem. One of the advantage of using interruption flag instead of creating your own signaling flag, or checking for elapsed time in the loop itself is that you can utilize the interruption support of other APIs.
For example, you might use Thread.sleep()
in your game. If you don't use the interruption mechanism, you must wait until sleep()
returns. If you do use the interruption mechanism, sleep()
will immediately return, throwing InterruptedException
, so your app. will be more responsive.
Whenever you catch InterruptedException
in your app. handle it as follows unless you have specific reasons:
try{
someMethod();
}catch(InterruptedException e){
//Restore interruption flag
Thread.currentThread.interrupt();
//If you have some clean up to do, do it here.
return;
}
Whenever the app. throws InterruptedException
you have to "restore" the interruption flag in order to relay the interruption message up the stack because InterruptedException
will "clear" the interruption flag (to false).
How are you keeping the game going in the first place? If, as I suspect it is with some sort of loop then why not allow the loop to terminate given the time condition?
精彩评论