Posted on 2006-06-22 11:08
壯士日志 閱讀(809)
評論(1) 編輯 收藏
package com.gf.rttw.warrants;
import java.util.Timer;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Main {
?static Logger logger = Logger.getLogger(Main.class);
?static {
??PropertyConfigurator.configure("log4jconfig.properties");
?}
?public static void main(String[] arg)
?{
??//get args from the console
??int gap=0;
??try {
???if(arg.length<1)
???{
????throw new Exception();
???}
???gap=Integer.parseInt(arg[0]);
???if((gap<1)||(gap>3600))
???{
????throw new Exception();
???}
??} catch (Exception e) {
???logger.error("PLEASE INPUT THE TIMER GAP 1-3600(SECONDS)");
???System.exit(1);
??}
???
??java.util.Timer timer= new Timer(true);?
??timer.schedule(
??new java.util.TimerTask()
??{
?? public void run()
??{
??? logger.debug("run task once");
??? //define any task you like
??}
??},
??0, gap*1000
??);
??try {
???Thread.sleep(Long.MAX_VALUE);
??} catch (InterruptedException e) {
???logger.fatal(e.getMessage());
??}
?}
}
上面的代碼很簡單,從命令行讀取一個參數(以秒為單位的時間間隔)然后該程序就會每隔一段時間打印一個"run task once";