Java定时重复执行程序

  • Post author:
  • Post category:java


Java定时重复执行程序



import


java.io.


*


;




import


java.util.


*


;




import


java.util.Timer;




import


java.util.TimerTask;







public




class


ScheduleRun




{


Timer timer;





public


ScheduleRun(


int


delaytime)




{



timer


=




new


Timer();


timer.schedule(


new


ScheduleRunTask(),


0


,delaytime


*




60




*




1000


);


//


推迟0秒执行, 间隔delaytime分钟重复运行.




//


timer.schedule(new ScheduleRunTask(),delaytime  * 1000);


//


推迟delaytime  秒后执行(只执行一次)






}








public




void


stop()




{



timer.cancel();


}










class


ScheduleRunTask


extends


TimerTask




{





int


numRunnings


=




5









public




void


runbat(


int


timeFortmat)




{



String cmd


=







cmd /c start D:/ScheduleRun/data/





+


timeFortmat


+





.bat





;





try




{



Process ps


=


Runtime.getRuntime().exec(cmd);


System.out.println(ps.getInputStream());



}





catch


(IOException ioe)




{



ioe.printStackTrace();


}





}










public




void


run()




{






if


(numRunnings


>


0


)




{



System.out.println(





running…..





);


runbat(


1340


);


numRunnings





;


}








else




{



System.out.println(





Task Finish!





);


timer.cancel();


//


Stop timer and we also can call System.exit




//


System.exit(0);


//


Stops everything






}





}





}










public




static




void


main(String[] args)




{





new


ScheduleRun(


5


);


}





}









版权声明:本文为leoyao2006原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。