How do you check the time of the program?



  • It's a sword. But the option is:

    st = System.nanoTime();
    //что-то делаю
    end = System.nanoTime();
    

    It is not appropriate to allow time after all methods have been completed. I need to get down from the start of the program, let's say one minute, if the program is done more, it's forced to come out, regardless of the completion of the methods. Maybe we should catch the time of the main flow.



  • For example, you can use the way. System.currentTimeMills():

    long start = System.currentTimeMillis();
    long end = start + 60*1000; // 60 секунд* 1000 мс/сек
    while (System.currentTimeMillis() < end) {
        // work hard, play hard
    }
    

    https://stackoverflow.com/questions/4252187/how-to-stop-execution-after-a-certain-time-in-java


    You can get out of the program. Thread:

    public class ExitAfterMinute {
        public static void main(String args[]) {
            new Thread(new Runnable() {
                public void run() {
                    try {
                        Thread.sleep(60000); // минута в милисекундах
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.exit(0);
                }
            }).start();
            while (true) {
                System.out.println("I'm doing something");
            }
        }
    }
    

    Either with a timer:

    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;
    

    public class TimedExit {
    Timer timer = new Timer();
    TimerTask exitApp = new TimerTask() {
    public void run() {
    System.exit(0);
    }
    };

    public TimedExit() {
        timer.schedule(exitApp, new Date(System.currentTimeMillis() + 60 * 1000));
    }
    

    }

    https://stackoverflow.com/questions/15747277/how-to-make-java-program-exit-after-a-couple-of-seconds


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2