java - Making a program run for 5 minutes -


so wanted try out bit timer , timertask classes.

i able line of code execute after 30 seconds elapsed. i've been trying line of code execute 5 minuets.

this tried

public static void main(string[] args) {     ( int = 0; <= 10; ++ )     {         timer timer = new timer();         timer.schedule( new timertask()         {             public void run()             {                 system.out.println("30 seconds later");             }         }, 30000         );     }    } 

i used number 10 in loop see if timer.schedule wait 30 seconds during next iteration of loop.

any idea how should go this? tried using schedule method parameter passed in period, made re-execute , never stopped.

the issue you're running scheduled timer runs on different thread - is, next iteration of for loop starts running after scheduling, not 30 seconds later. looks code starts ten timers @ once, means should print (roughly) 30 seconds later, @ once.

you on right track when tried using recurring version of schedule (with third parameter). noted, isn't quite want because runs indefinitely. however, timer does have cancel method prevent subsequent executions.

so, should try like:

final timer timer = new timer(); // note timer has been declared final, allow use in anon. class below timer.schedule( new timertask() {     private int = 10;     public void run()     {         system.out.println("30 seconds later");         if (--i < 1) timer.cancel(); // count down ten times, cancel     } }, 30000, 30000 //note second argument repetition ); 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -