boy_wh520的专栏

烟在风中飘荡

Java定时重复执行程序

import java.io.*;
import java.util.*;
import java.util.Timer;
import java.util.TimerTask;

public class ScheduleRun ...{    
    Timer timer;
    
public ScheduleRun(int delaytime)...{
        timer 
= new Timer();
        timer.schedule(
new ScheduleRunTask(),0,delaytime * 60 * 1000);   //推迟0秒执行, 间隔delaytime分钟重复运行. 
        
//timer.schedule(new ScheduleRunTask(),delaytime  * 1000);     //推迟delaytime  秒后执行(只执行一次)
    }

    
public void stop()...{
        timer.cancel();
    }

    
    
class ScheduleRunTask extends TimerTask...{
        
int numRunnings = 5
            
        
public void runbat(int timeFortmat)...{
            String cmd 
= "cmd /c start D:/ScheduleRun/data/"+timeFortmat+".bat";
            
try...{
                Process ps 
= Runtime.getRuntime().exec(cmd);
                System.out.println(ps.getInputStream());
            }
 catch(IOException ioe)...{
                ioe.printStackTrace();
            }

        }

        
        
public void run() ...{
            
if(numRunnings>0)...{
              System.out.println(
"running.....");
              runbat(
1340);
              numRunnings
--;
          }

          
else...{
            System.out.println(
"Task Finish!");
            timer.cancel(); 
//Stop timer and we also can call System.exit
            
//System.exit(0);   //Stops everything
              }
     
        }
       
    }

    
    
public static void main(String[] args)...{
        
new ScheduleRun(5);        
    }

}

 
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭