Making a program run for 5 minutes

前端 未结 5 2078
隐瞒了意图╮
隐瞒了意图╮ 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

    Your solution is pretty close to working, you just have to multiply the delay by the counter (in your case, i):

    public static void main(String[] args)
    {
        for (int i = 1; i <= 10; i++) // start i at 1 for initial delay
        {
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                public void run()
                {
                    System.out.println("30 Seconds Later");
                }
            }, 30000 * i); // 5 second intervals
        }
    }
    

提交回复
热议问题