java 计时器应用

mport java.util.TimerTask;   
import java.util.Calendar;   

public class MyTask extends TimerTask 
{   
     private static final int C_SCHEDULE_HOUR = 15;   
     private static boolean isRunning = false;   

     public MyTask() 
     {   
     }   

     public void run() 
     {   
         Calendar cal = Calendar.getInstance();   
         if (!isRunning) 
         {   
             //if (C_SCHEDULE_HOUR == cal.get(Calendar.HOUR_OF_DAY)) 
             {   
                 isRunning = true;   
                 System.out.println(new java.util.Date() + "         任务开始");   
                 for (int i = 0 ; i < 100 ; i++ ) 
                 { 
                     System.out.println(new java.util.Date() + "           任务完成" + i + "/" + 100 );   
                 }   
                 isRunning = false;   
                 System.out.println(new java.util.Date() + "        所有任务完成!");   
             }   
         }   
         else   
         {   
             System.out.println(new java.util.Date() + "          任务退出!!!");   
         }   
     }   

=====================================Test=========================================== 
import java.util.TimerTask; 
import java.util.Timer;   
import javax.servlet.*; 
public class Test{ 
     static Timer timer = null; 
     public static void main(String[] args){ 
         timer = new Timer(true); 
         System.out.println(new java.util.Date() + "       计时器已经启动..."); 
         timer.schedule(new MyTask() , 0 , 2*60*1000);//每2分钟执行一次 
         System.out.println(new java.util.Date() + "           计时器执行一次!!!!!"); 
     } 

 

原文地址:http://hi.baidu.com/dotfire/item/76d337c87553bb13515058bb

你可能感兴趣的:(java,timer)