java重置Timer执行频率

 1 public class BallUtil {
 2     public static  Timer fisTimer ;
 3     
 4     public static void fisStartBall(){
 5         long firstTime = 2000l;
 6         long time = 2000l;
 7         if(fisTimer==null){
 8             fisTimer = new Timer();
 9         }
10         fisTimer.scheduleAtFixedRate(new TimerTask(){
11             public void run(){
12                 System.out.println("time="+time+"-----firstTime="+"定时器执行"+"----type="+type);
13                 Date date = new Date();
14                 int hour = date.getHours();
15                 if(hour==9){
16                         System.out.println("重置频率");
17                         stop();//关闭后再开启
18                         start();
19                         //直接更新新的时间
20                         //Long newTime = patt.getInt("INTERVDAY")*60l*1000;
21                         //resetPeriod(newTime);
22                 }
23             }
24             //此方法也可以重置频率,直接传入新的时间
25             public void resetPeriod(final long time) throws IllegalArgumentException, IllegalAccessException{
26                 Field[] fields = this.getClass().getSuperclass() .getDeclaredFields();  
27                    for (Field field : fields)  
28                    {  
29                        if (field.getName().endsWith("period"))  
30                        {  
31                            if (!field.isAccessible())  
32                            {  
33                                field.setAccessible(true);  
34                            }  
35                            field.set(this, time);  
36                        }  
37                    }  
38             }
39         }, firstTime, time);
40     }
41     public  static void start(){
42             fisStartBall();
43     }
44     
45     public static void stop(){
46             fisTimer.cancel();
47     }
48 }

 

posted @ 2016-12-27 12:46  suruozhong  阅读(2404)  评论(0编辑  收藏  举报