java timer线程结束_关于java中使用timer进行线程调度

在使用timer进行线程调度时出现这样一种问题,求大侠们指教

主程序入口

public class App {

public static void main(String[] args) throws Exception {

Timer fileTransportTimer=new Timer("fileTransportTimer");

fileTransportTimer.scheduleAtFixedRate(new FileTransportTimer(), 3000, 1000*30);

}

}

定时任务线程

public class FileTransportTimer extends TimerTask{

public static boolean isRun = false;

public void run() {

if (isRun) {

return;

}

isRun = true;

SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss");

logger.info("开始扫描并发送文件!");

try{

//扫描目录,判断是否有待发送的文件

String path=AppStart.cache.getLocalFilePath();

File root=new File(path);

if(!root.isDirectory()){

throw new Exception("file root is not a directory");

}

File[] files=root.listFiles();

for(int i=0;i

File tmp=files[i];

if(tmp.isFile()){

try {MQHandler.sendFile(tmp);

} catch (Exception e) {

e.printStackTrace();

}

}

break;

}

logger.info("发送文件任务结束!");

}catch(Exception e){

logger.error("发送文件任务错误!");

e.printStackTrace();

}finally{

isRun=false;

}

}

}

这里已经进行了运行状态的控制,程序确实也是这样的一个执行结果,但每次在执行完成后

都会有这样的日志输出

2013-02-21 16:01:08 com.rf.thread.FileTransportTimer开始扫描并发送文件!

2013-02-21 16:01:08 com.rf.util.MQHandler文件War3x.mpq高优先级

2013-02-21 16:05:20 com.rf.thread.FileTransportTimer发送文件任务结束!

2013-02-21 16:05:20 com.rf.thread.FileTransportTimer开始扫描并发送文件!

2013-02-21 16:05:20 com.rf.thread.FileTransportTimer发送文件任务结束!

2013-02-21 16:05:20 com.rf.thread.FileTransportTimer开始扫描并发送文件!

2013-02-21 16:05:20 com.rf.thread.FileTransportTimer发送文件任务结束!

2013-02-21 16:05:20 com.rf.thread.FileTransportTimer开始扫描并发送文件!

2013-02-21 16:05:20 com.rf.thread.FileTransportTimer发送文件任务结束!

2013-02-21 16:05:20 com.rf.thread.FileTransportTimer开始扫描并发送文件!

2013-02-21 16:05:20 com.rf.thread.FileTransportTimer发送文件任务结束!

2013-02-21 16:05:20 com.rf.thread.FileTransportTimer开始扫描并发送文件!

2013-02-21 16:05:20 com.rf.thread.FileTransportTimer发送文件任务结束!

2013-02-21 16:05:20 com.rf.thread.FileTransportTimer开始扫描并发送文件!

2013-02-21 16:05:20 com.rf.thread.FileTransportTimer发送文件任务结束!

2013-02-21 16:05:20 com.rf.thread.FileTransportTimer开始扫描并发送文件!

2013-02-21 16:05:20 com.rf.thread.FileTransportTimer发送文件任务结束!

2013-02-21 16:05:20 com.rf.thread.FileTransportTimer开始扫描并发送文件!

2013-02-21 16:05:20 com.rf.thread.FileTransportTimer发送文件任务结束!

2013-02-21 16:05:38 com.rf.thread.FileTransportTimer开始扫描并发送文件!

2013-02-21 16:05:38 com.rf.thread.FileTransportTimer发送文件任务结束!

处理文件的时候线程正在运行,新的定时任务不会执行到文件处理的代码部分就会return掉,但当第一个定时任务执行完成后,一下子就打印出了好多后面的这些日志,这点不太明白???

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值