WEB开发网
开发学院WEB开发Jsp Java语言中Timer类的简洁用法(二) 阅读

Java语言中Timer类的简洁用法(二)

 2008-01-05 19:20:14 来源:WEB开发网   
核心提示:实现计划框架在上一节,我们学习了如何使用计划框架,Java语言中Timer类的简洁用法(二),并将它与 java 定时器框架进行了比较,下面,清单 5 和 6 显示了这两个类的源代码: 清单 5. Scheduler package org.tiling.scheduling;import java.util.Date

  实现计划框架
  在上一节,我们学习了如何使用计划框架,并将它与 java 定时器框架进行了比较。下面,我将向您展示如何实现这个框架。除了 清单 3 中展示的 ScheduleIterator 接口,构成这个框架的还有另外两个类 —— Scheduler 和 SchedulerTask 。这些类实际上在内部使用 Timer 和 SchedulerTask,因为计划其实就是一系列的单次定时器。
  
  清单 5 和 6 显示了这两个类的源代码:
  
  清单 5. Scheduler
  
  package org.tiling.scheduling;
  
  import java.util.Date;
  import java.util.Timer;
  import java.util.TimerTask;
  
  public class Scheduler {
  
    class SchedulerTimerTask extends TimerTask {
      PRivate SchedulerTask schedulerTask;
      private ScheduleIterator iterator;
      public SchedulerTimerTask(SchedulerTask schedulerTask,
          ScheduleIterator iterator) {
        this.schedulerTask = schedulerTask;
        this.iterator = iterator;
      }
      public void run() {
        schedulerTask.run();
        reschedule(schedulerTask, iterator);
      }
    }
  
    private final Timer timer = new Timer();
  
    public Scheduler() {
    }
  
    public void cancel() {
      timer.cancel();
    }
  
    public void schedule(SchedulerTask schedulerTask,
        ScheduleIterator iterator) {
  
      Date time = iterator.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, iterator);
          timer.schedule(schedulerTask.timerTask, time);
        }
      }
    }
  
    private void reschedule(SchedulerTask schedulerTask,
        ScheduleIterator iterator) {
  
      Date time = iterator.next();
      if (time == null) {
        schedulerTask.cancel();
      } else {
        synchronized(schedulerTask.lock) {
          if (schedulerTask.state != SchedulerTask.CANCELLED) {
            schedulerTask.timerTask =
              new SchedulerTimerTask(schedulerTask, iterator);
            timer.schedule(schedulerTask.timerTask, time);
          }
        }
      }
    }
  
  }
  
  清单 6 显示了 SchedulerTask 类的源代码:
  
  package org.tiling.scheduling;
  
  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;
  
    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 scheduledExecutionTime() {
      synchronized(lock) {
       return timerTask == null ? 0 : timerTask.scheduledExecutionTime();
      }
    }
  
  }
   就像煮蛋计时器,Scheduler 的每一个实例都拥有 Timer 的一个实例,用于提供底层计划。

Tags:Java Timer

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接