java timer时间调度调试理解
2014-10-13 17:12
232 查看
先写一个timer并通过sechedule计划函数执行自己定义的timertask时间调度任务 private Timer mTimer = null; mTimer = new Timer(); mTimer.schedule(new MyTimerTask(), 1000 ,TIME * 1000); class MyTimerTask extends TimerTask{ @Override public void run() { mHandler.sendMessage(mHandler.obtainMessage()); } }最先函数进入到这里
/** * Schedule a task for repeated fixed-delay execution after a specific delay. * * @param task * the task to schedule. * @param delay * amount of time in milliseconds before first execution. * @param period * amount of time in milliseconds between subsequent executions. * @throws IllegalArgumentException * if {@code delay < 0} or {@code period <= 0}. * @throws IllegalStateException * if the {@code Timer} has been canceled, or if the task has been * scheduled or canceled. */ public void schedule(TimerTask task, long delay, long period) { if (delay < 0 || period <= 0) { throw new IllegalArgumentException(); } scheduleImpl(task, delay, period, false); }<pre name="code" class="java">/* * Schedule a task. */ private void scheduleImpl(TimerTask task, long delay, long period, boolean fixed) { synchronized (impl) { if (impl.cancelled) { throw new IllegalStateException("Timer was canceled"); } long when = delay + System.currentTimeMillis(); if (when < 0) { throw new IllegalArgumentException("Illegal delay to start the TimerTask: " + when); } synchronized (task.lock) { if (task.isScheduled()) { throw new IllegalStateException("TimerTask is scheduled already"); } if (task.cancelled) { throw new IllegalStateException("TimerTask is canceled"); } task.when = when; task.period = period; task.fixedRate = fixed; } // insert the newTask into queue impl.insertTask(task); } }
修改task的属性设定好第一次的执行会在当前时间delay毫秒后,每隔period毫秒这行一次。
<pre name="code" class="java">为了保证所有的时间计划任务的执行都不会发生冲突,使用synchronized限定同一时间只能有一个线程调用 scheduleImpl
private void insertTask(TimerTask newTask) { // callers are synchronized tasks.insert(newTask); this.notify(); }
接下来就要将自己定义的TimerTask插入到一个数组中
private int DEFAULT_HEAP_SIZE = 256; private int size = 0; private TimerTask[] timers = new TimerTask[DEFAULT_HEAP_SIZE];默认设置用于保存timertask时间计划任务的堆的大小事256
public void insert(TimerTask task) { if (timers.length == size) { TimerTask[] appendedTimers = new TimerTask[size * 2]; System.arraycopy(timers, 0, appendedTimers, 0, size); timers = appendedTimers; } timers[size++] = task; upHeap(); }当timertask堆中没有计划任务,或者堆中的任务数量已经达到了其前一次设定的堆的大小则将堆的大小升级为以前堆的大小的一倍
<pre name="code" class="java">/** * Contains scheduled events, sorted according to * {@code when} field of TaskScheduled object. */ private TimerHeap tasks = new TimerHeap();
更新计划任务堆,用根据计划起始时间二分排序对新插入的计划任务timertask出入到合适位置
private void upHeap() { int current = size - 1; int parent = (current - 1) / 2; while (timers[current].when < timers[parent].when) { // swap the two TimerTask tmp = timers[current]; timers[current] = timers[parent]; timers[parent] = tmp; // update pos and current current = parent; parent = (current - 1) / 2; } }
相关文章推荐
- 用Java Timer API进行时间调度开发的相关注意点
- 用Java Timer API进行时间调度开发的相关注意点
- java应用之系统时间的获取和类Timer的核心代码
- java 定时器 timer 时间监听 初学
- Java定时调度之Timer/TimerTask
- 深入理解Java:SimpleDateFormat安全的时间格式化
- c2java 第3篇 红黑树删除的理解和jdb调试初步(2014.04.03更新)
- 简单的使用java Timer调度任务
- 黑马程序员—java之银行调度系统理解
- 深入理解Java:SimpleDateFormat安全的时间格式化
- [Java 11] Timer 定时调度 入门
- JAVA中Timer定时器调度方法
- java timer 实现在指定时间执行方法
- JAVA时间的理解
- Java常用类库--定时调度(Timer、TimerTask)
- Java 定时调度(Timer和TimerTask)
- 理解 Java 时间, 日期
- 深入理解Java调试体系
- 深入理解Java:SimpleDateFormat安全的时间格式化
- 理解Java ClassLoader机制 |用Java说话,人气战胜时间!Come On