java重設定時器頻率

來源:互聯網
上載者:User

標籤:type   方法   tin   timertask   with   system   name   res   start   

 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 }

 

java重設定時器頻率

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.