Making a program run for 5 minutes

前端 未结 5 1975
隐瞒了意图╮
隐瞒了意图╮ 2021-01-26 14:03

So I wanted to try out something for a bit with the Timer and TimerTask classes.

I was able to get a line of code to execute after 30 seconds elapsed. What I\'ve been t

5条回答
  •  一生所求
    2021-01-26 14:18

    here's a workaround I'm ashamed of presenting:

    package test;
    
    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class FiveMinutes {
    
        private static int count = 0;
        // main method just to add example
        public static void main(String[] args) {
    
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
    
                @Override
                public void run() {
                    System.out.println("Count is: " + count);
                    if (count == 1) {
                        System.err.println("... quitting");
                        System.exit(0);
                    }
                    count++;
                }
            },
            // starting now
            new Date(),
            // 5 minutes
            300000l
            );  
        }
    }
    

    Also please note that the application might not run exactly 5 minutes - see documentation for TimerTask.

提交回复
热议问题