posts - 25, comments - 69, trackbacks - 0, articles - 2

java.util.Timer原来这这么好用

Posted on 2006-06-22 11:08 壮士日志 阅读(807) 评论(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";

Feedback

# re: java.util.Timer原来这这么好用  回复  更多评论   

2006-10-09 13:39 by mtlai
gdfgdf

只有注册用户登录后才能发表评论。


网站导航: