zhijin 发表于 2013-1-27 04:50:42

Java定时重复执行程序

Java定时重复执行程序
 
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.io.*;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.util.*;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.util.Timer;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.util.TimerTask;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gifpublic class ScheduleRun ...{    
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    Timer timer;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    public ScheduleRun(int delaytime)...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        timer = new Timer();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        timer.schedule(new ScheduleRunTask(),0,delaytime * 60 * 1000);   //推迟0秒执行, 间隔delaytime分钟重复运行. 
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        //timer.schedule(new ScheduleRunTask(),delaytime  * 1000);     //推迟delaytime  秒后执行(只执行一次)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    public void stop()...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        timer.cancel();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    class ScheduleRunTask extends TimerTask...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        int numRunnings = 5
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif        public void runbat(int timeFortmat)...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            String cmd = "cmd /c start D:/ScheduleRun/data/"+timeFortmat+".bat";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif            try...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                Process ps = Runtime.getRuntime().exec(cmd);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                System.out.println(ps.getInputStream());
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif            } catch(IOException ioe)...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                ioe.printStackTrace();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif            }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif        public void run() ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif            if(numRunnings>0)...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif              System.out.println("running.....");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif              runbat(1340);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif              numRunnings--;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif          }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif          else...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            System.out.println("Task Finish!");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            timer.cancel(); //Stop timer and we also can call System.exit
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            //System.exit(0);   //Stops everything
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif              }     
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }       
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    public static void main(String[] args)...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        new ScheduleRun(5);        
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
页: [1]
查看完整版本: Java定时重复执行程序