zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    C#的4个基本技巧
    如何使用 Visual C# 加密和解密文件
    C#中的反射
    C#集成FastDFS断点续传
    NETCORE TimeJob定时任务的使用
    C#使用ActiveMQ实例
    hadoop+hbase适合存储海量小图片吗?
    windows环境下elasticsearch安装教程(超详细)
    C#:C#调用WebService
    .NETCore远程调用
  • 原文地址:https://www.cnblogs.com/suruozhong/p/6225451.html
Copyright © 2011-2022 走看看