Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3348110
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:30:10+00:00 2026-05-18T01:30:10+00:00

I am designing a Card based Game in Java. I have implemented the game

  • 0

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 want 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:30:11+00:00Added an answer on May 18, 2026 at 1:30 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is where I am at presently. I am designing a card game with
I'm new to designing OO systems. I have a simple Flash Cards app where
We are designing asp.net web application in wcsf. Web application will be deployed to
Is it worth designing a system to expect test accounts and products to be
I'm designing the 2nd major iteration of a relational database for a franchise's CRM
I am designing a library for my application, that wraps Windows smartcard subsystem. Physically,
We have an iPhone application created by an external consultancy that we're planning to
What would be the best way to display & program simple game board (say
I am relatively new to game development so I decided I wanted to create
I'm currently creating an application for a customer that will allow them to automatically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.