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";