码迷,mamicode.com
首页 > 其他好文 > 详细

Timer

时间:2015-09-17 11:28:12      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

public class Reminder{
    Timer timer;
    protected int start=0;
    protected int end=0;

    public Reminder(int s,int e,int frequency){
       start = s;
       end = e;
       timer = new Timer();
       timer.schedule(new ReminderTask(),0,frequency*1000);
    }
    public class ReminderTask extends TimerTask{
        
        public void run(){
            
            System.out.println("doing something");
            Calendar cal = Calendar.getInstance();
            if(cal.get(Calendar.HOUR_OF_DAY) > end){
                int hour = cal.get(Calendar.HOUR_OF_DAY);   
                //计算需要睡眠多久
                while(true){
                    try {
                        Thread.sleep((start+24-hour)*1000);
                        break;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            }
        }

        
        }

public static void main(String args[]){
    
        new Reminder(8,16,1);
    }
}

 

Timer

标签:

原文地址:http://www.cnblogs.com/jinjixia/p/4815614.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!