已遷址

          已遷址http://www.cnblogs.com/live365wang/

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            28 隨筆 :: 0 文章 :: 1 評論 :: 0 Trackbacks
          代碼1:
          package com.tg.email;

          import java.text.SimpleDateFormat;
          import java.util.Calendar;
          import java.util.Date;
          import java.util.Timer;
          import java.util.TimerTask;

          public class Timeer {

              
          public static void showTimer() {
                  TimerTask task 
          = new TimerTask() {
                      @Override
                      
          public void run() {
                          System.out.print(
          new SimpleDateFormat("HH:mm:ss").format(System.currentTimeMillis()));
                          System.out.println(
          "執行定時任務!!!");
                      }
                  };

                  Calendar calendar 
          = Calendar.getInstance();
                  
          int year = calendar.get(Calendar.YEAR);
                  
          int month = calendar.get(Calendar.MONTH);
                  
          int day = calendar.get(Calendar.DAY_OF_MONTH);
                  
          /*** 時間自己修改 ***/
                  System.out.println(
          "year:"+year);
                  System.out.println(
          "month:"+(month));
                  System.out.println(
          "day:"+day);
                  
                  calendar.set(year, month, day, 
          130003);    //主要改這里,年、月、日、時、分、秒
                  Date date = calendar.getTime();
                  Timer timer 
          = new Timer();
                  timer.schedule(task, date,
          1000);
              }


              
          public static void main(String[] args) {
                  showTimer();
              }
          }
          代碼2:
          package
           com.lzw.schedule;

          import java.util.TimerTask;

          public abstract class SchedulerTask implements Runnable
          {
              
          final Object lock = new Object();

              
          int state = VIRGIN;

              
          static final int VIRGIN = 0;

              
          static final int SCHEDULED = 1;

              
          static final int CANCELLED = 2;

              TimerTask timerTask 
          = null;


              
          protected SchedulerTask()
              {

              }


              
          public abstract void run();


              
          public boolean cancel()
              {
                  
          synchronized (lock)
                  {
                      
          if (timerTask != null)
                      {
                          timerTask.cancel();
                      }
                      
          boolean result = (state == SCHEDULED);

                      state 
          = CANCELLED;

                      
          return result;
                  }
              }


              
          public long scheduleExecutionTime()
              {
                  
          synchronized (lock)
                  {
                      
          return timerTask == null ? 0 : timerTask.scheduledExecutionTime();
                  }
              }
          }
          代碼3:
          package
           com.lzw.schedule;

          public class AlarmClock
          {
              
          private final Scheduler scheduler = new Scheduler();

              
          private final int hourofDay, minute, second;


              
          public AlarmClock(int hourOfDay, int minute, int second)
              {
                  
          this.hourofDay = hourOfDay;
                  
          this.minute = minute;
                  
          this.second = second;
              }


              
          public void start()
              {
                  scheduler.schedule(
          new SchedulerTask()
                  {
                      
          public void run()
                      {
                          System.out.println(
          "時間到");
                      }
                  }, 
          new DailyIterator(hourofDay, minute, second));
              }


              
          public static void main(String[] args)
              {
                  AlarmClock alarmClock 
          = new AlarmClock(17580);
                  alarmClock.start();
              }
          }
          代碼4:
          package
           com.lzw.schedule;

          import java.util.Date;
          import java.util.Timer;
          import java.util.TimerTask;

          public class Scheduler
          {
              
          class SchedulerTimerTask extends TimerTask
              {
                  
          private SchedulerTask schedulerTask = null;

                  
          private ScheduleIterator scheduleIterator = null;


                  
          public SchedulerTimerTask(SchedulerTask schedulerTask,
                                            ScheduleIterator scheduleIterator)
                  {
                      
          this.schedulerTask = schedulerTask;
                      
          this.scheduleIterator = scheduleIterator;
                  }


                  
          public void run()
                  {
                      schedulerTask.run();
                      reschedule(schedulerTask, scheduleIterator);
                  }
              }

              
          private final Timer timer = new Timer();


              
          public Scheduler()
              {
              }


              
          public void cancel()
              {
                  timer.cancel();
              }


              
          public void schedule(SchedulerTask schedulerTask,
                                   ScheduleIterator scheduleIterator)
              {
                  Date time 
          = scheduleIterator.next();

                  
          if (time == null)
                  {
                      schedulerTask.cancel();
                  }
                  
          else
                  {
                      
          synchronized (schedulerTask.lock)
                      {
                          
          if (schedulerTask.state != SchedulerTask.VIRGIN)
                          {
                              
          throw new IllegalStateException("Task already scheduled or cancelled");
                          }
                          
                          schedulerTask.state 
          = SchedulerTask.SCHEDULED;

                          schedulerTask.timerTask 
          = new SchedulerTimerTask(schedulerTask,
                                                                           scheduleIterator);

                          timer.schedule(schedulerTask.timerTask, time);
                      }
                  }
              }


              
          private void reschedule(SchedulerTask schedulerTask,
                                      ScheduleIterator scheduleIterator)
              {
                  Date time 
          = scheduleIterator.next();

                  
          if (time == null)
                  {
                      schedulerTask.cancel();
                  }
                  
          else
                  {
                      
          synchronized (schedulerTask.lock)
                      {
                          
          if (schedulerTask.state != SchedulerTask.CANCELLED)
                          {
                              schedulerTask.timerTask 
          = new SchedulerTimerTask(schedulerTask,
                                                                               scheduleIterator);

                              timer.schedule(schedulerTask.timerTask, time);
                          }
                      }
                  }
              }
          }
          代碼5:
          package
           com.lzw.schedule;

          import java.util.Calendar;
          import java.util.Date;

          public interface ScheduleIterator
          {
              
          public Date next();
          }


          class DailyIterator implements ScheduleIterator
          {

              
          private final Calendar calendar = Calendar.getInstance();

              
          public DailyIterator(int hourOfDay, int minute, int second, Date date)
              {
                  calendar.setTime(date);
                  calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                  calendar.set(Calendar.MINUTE, minute);
                  calendar.set(Calendar.SECOND, second);
                  calendar.set(Calendar.MILLISECOND, 
          0);
                  
                  
          if (!calendar.getTime().before(date)) {
                      calendar.add(Calendar.DATE, 
          -1);
                  }
              }
              
              
          public DailyIterator(int hourOfDay, int minute, int second)
              {
                  Date date 
          = new Date();
                  calendar.setTime(date);
                  calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                  calendar.set(Calendar.MINUTE, minute);
                  calendar.set(Calendar.SECOND, second);
                  calendar.set(Calendar.MILLISECOND, 
          0);
                  
                  
          if (!calendar.getTime().before(date)) {
                      calendar.add(Calendar.DATE, 
          -1);
                  }
              }
              
              
          public Date next() {
                  calendar.add(Calendar.DATE, 
          1);
                  
          return calendar.getTime();
              }
          }
          代碼6:
          package
           com.lzw;

          import java.util.Calendar;
          import java.util.Date;
          import java.util.Timer;
          import java.util.TimerTask;

          public class Test
          {
              
          private Calendar startSee = null//開始看病時間


              
          public Test()
              {
                  startSee 
          = Calendar.getInstance();
                  System.out.println(
          "感冒了." + startSee.getTime().toLocaleString());
              }
              
              
          public Date getScheduleDate(int day)
              {
          //        startSee.add(Calendar.DATE, 1);   //按天來計算
                  startSee.add(Calendar.SECOND, day); //按秒來計算
                  return startSee.getTime();
              }

              
          public void start(int day)
              {
                 
          final Timer timer = new Timer();
                 
                  timer.schedule(
          new TimerTask()
                  {
                      
          public void run()
                      {
                          System.out.println(
          "回訪啦.." + new Date().toLocaleString());
                          
                          timer.cancel();
                      }
                  }, getScheduleDate(day));
              }
              
              
          public static void main(String[] args)
              {
                  
          //由于timer調系統時間不太起作用,所以拿秒來做測試.
                  Test test = new Test();
                  test.start(
          10);  
                  test.start(
          15);  
                  test.start(
          20);  
                  test.start(
          30);  
              }
          }
          部分代碼來源于http://topic.csdn.net/u/20100705/18/73d51340-631f-4b9a-89e7-3125df30fec9.html?seed=351074017&r=66736573#r_66736573
          posted on 2011-02-24 13:27 已遷址 閱讀(228) 評論(1)  編輯  收藏 所屬分類: Java

          評論

          # re: Timer 及 TimerTask 相關使用代碼 2011-06-18 18:20 jksnu1@gmail.com
          This is very much useful information about java.util.Timer.
          Really its very fruitful.

          I also liked the following links regarding java job scheduling

          Java Job Scheduling with java.util.Timer
          http://jksnu.blogspot.com/2011/02/java-job-scheduling.html" target="_new" rel="nofollow">http://jksnu.blogspot.com/2011/02/java-job-scheduling.html

          Quartz Scheduling with JSP-Servlet
          http://jksnu.blogspot.com/2011/03/quartz-framework-implementation-with.html" target="_new" rel="nofollow">http://jksnu.blogspot.com/2011/03/quartz-framework-implementation-with.html

          Quartz Scheduling with Spring Framework
          http://jksnu.blogspot.com/  回復  更多評論
            

          主站蜘蛛池模板: 涪陵区| 遵义县| 南宁市| 台中县| 芷江| 横山县| 揭东县| 彰化市| 克什克腾旗| 青阳县| 乌兰县| 杭州市| 贺州市| 乌恰县| 麻阳| 博野县| 如皋市| 桦南县| 保定市| 兴隆县| 岳池县| 九龙坡区| 罗定市| 岳西县| 永和县| 尖扎县| 波密县| 秭归县| 密山市| 贺兰县| 冷水江市| 株洲市| 津南区| 淳安县| 简阳市| 泽普县| 江源县| 资兴市| 探索| 新昌县| 樟树市|