关于TimerTask
import java.util.Timer;
import java.util.TimerTask;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class BatchLoaderTimerTask
extends TimerTask {
public String strRoot = null; //应用服务器的根目录
public String strDir = null; //要入库的文件所在的目录
BatchLoader _batchLoader = null;
int i = 1;
public BatchLoaderTimerTask(String strRoot, String strDir)
throws Exception {
this.strRoot = strRoot;
this.strDir = strDir;
_batchLoader = new BatchLoader(strRoot);
}
public void run() {
try {
System.out.println("第 " + i++ + " 次进入TimerTask" );
boolean isLoadFinished = _batchLoader.getIsLoadFinished();
//上一次装载完成 做新的装载任务
if(isLoadFinished) {
_batchLoader.setIsLoadFinished(false);
_batchLoader.doBatchLoad(strDir);
_batchLoader.setIsLoadFinished(true);
}
else {
System.out.println("上一次装载未完,本次不工作!!!!");
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.out.println("Usage: ");
System.out.println(
" java BatchLoaderTimerTask <dir of WebApplication> <dir of docs>");
System.out.println("");
return;
}
String strRoot = args[0];
String strDir = args[1];
long delay = 0;
long period = 1000; //间隔1秒
Timer _timer = new Timer();
BatchLoaderTimerTask task = null;
try {
task = new BatchLoaderTimerTask(strRoot, strDir);
}
catch (Exception ex) {
ex.printStackTrace();
throw ex;
}
_timer.scheduleAtFixedRate(task, delay, period);
}
}
我的问题是: 当定时器启动后,应每1秒进行一次run的动作 ,
现在的情况是:当前一个run没执行完时,第二次不会执行。只有等到前一次执行完了。才会执行第二次,
这是定时器吗?