关于TimerTask

liuaaron 2003-08-19 02:12:13
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没执行完时,第二次不会执行。只有等到前一次执行完了。才会执行第二次,

这是定时器吗?
...全文
30 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dingzhaofeng 2003-08-19
  • 打赏
  • 举报
回复
路过...
liuaaron 2003-08-19
  • 打赏
  • 举报
回复
我写的 有错吗,还是调用的方法不正确。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧