JOTM中定时器的源码分析

81 篇文章 0 订阅
在Jotm中看到一个很齐全的定时器,贴出来以防备用;

  1. package org.objectweb.jotm;
  2. import java.util.Vector;
  3. /**
  4.  *
  5.  *对计时器列表中的计时器进行倒计时
  6.  */
  7. class Clock extends Thread {
  8.     private TimerManager tmgr;
  9.     public Clock(TimerManager tmgr) {
  10.         super("JotmClock");
  11.         if (TraceTm.jta.isDebugEnabled()) {
  12.             TraceTm.jta.debug("Clock constructor");
  13.         }
  14.         this.tmgr = tmgr;
  15.     }
  16.     public void run() {
  17.         tmgr.clock();
  18.     }
  19. }
  20. /**
  21.  *
  22.  *出去计时器列表中的过期计时器,如倒计时为负数
  23.  */
  24. class Batch extends Thread {
  25.     private TimerManager tmgr;
  26.     public Batch(TimerManager tmgr) {
  27.         super("JotmBatch");
  28.         if (TraceTm.jta.isDebugEnabled()) {
  29.             TraceTm.jta.debug("Batch constructor");
  30.         }
  31.         this.tmgr = tmgr;
  32.     }
  33.     public void run() {
  34.         tmgr.batch();
  35.     }
  36. }
  37. /**
  38.  *含有两个计时器列表,并且含有两个线程,一个线程进行计时器的倒计时,
  39.  *一个线程移除过期计时器并且执行计时器的监听器动作;
  40.  */
  41. public class TimerManager {
  42.     // threads managing the service.
  43.     private static Batch batchThread;
  44.     private static Clock clockThread;
  45.     // lists
  46.     //计时器列表
  47.     private Vector timerList = new Vector();
  48.     //过期计时器列表
  49.     private Vector expiredList = new Vector();
  50.     
  51.     //单例
  52.     private static TimerManager unique = null;
  53.     private static boolean shuttingdown = false;
  54.     /**
  55.      * Constructor
  56.      */
  57.     private TimerManager() {
  58.         // launch threads for timers
  59.         batchThread = new Batch(this);
  60.         batchThread.setDaemon(true);
  61.         batchThread.start();
  62.         clockThread = new Clock(this);
  63.         clockThread.setDaemon(true);
  64.         clockThread.start();
  65.     }
  66.     /**
  67.      * 这个时间管理器是一个单例类;
  68.      */
  69.     public static TimerManager getInstance() {
  70.         if (unique == null)
  71.             unique = new TimerManager();
  72.         return unique;
  73.     }
  74.     //停止时间管理器中的计时器;
  75.     public static void stop(boolean force) {
  76.         if (TraceTm.jta.isDebugEnabled()) {
  77.             TraceTm.jta.debug("Stop TimerManager");
  78.         }
  79.         TimerManager tmgr = getInstance();
  80.         shuttingdown = true;
  81.         while (clockThread.isAlive() || batchThread.isAlive()) {
  82.             try {
  83.                 Thread.sleep(100);
  84.             } catch (InterruptedException e) {
  85.                 break;
  86.             }
  87.         }
  88.         if (TraceTm.jta.isDebugEnabled()) {
  89.             TraceTm.jta.debug("TimerManager has stopped");
  90.         }
  91.     }
  92.     public static void stop() {
  93.         stop(true);
  94.     }
  95.     /**
  96.      * cney speed up the clock x1000 when shutting down
  97.      * update all timers in the list
  98.      * each timer expired is put in a special list of expired timers
  99.      * they will be processed then by the Batch Thread.
  100.      */
  101.     public void clock() {
  102.         //无限循环
  103.         while (true) {
  104.             try {
  105.                 //线程休息一秒
  106.                 Thread.sleep(shuttingdown?1:1000);  // 1 second or 1ms shen shuttingdown
  107.                 // Thread.currentThread().sleep(shuttingdown?1:1000);    // 1 second or 1ms shen shuttingdown
  108.                 synchronized(timerList) {
  109.                     int found = 0;
  110.                     boolean empty = true;
  111.                     for (int i = 0; i < timerList.size(); i++) {
  112.                         TimerEvent t = (TimerEvent) timerList.elementAt(i);
  113.                         //如果没有活动的计时器,那么计时器队列为空;
  114.                         if (!t.isStopped()) {
  115.                             empty = false;
  116.                         }
  117.                         //如果计时器过期
  118.                         if (t.update() <= 0) {
  119.                             //从计时器队列中移除
  120.                             timerList.removeElementAt(i--);
  121.                             if (t.valid()) {
  122.                                 //该计时器存在计时器监听器的话,把这个计时器加入过期计时器
  123.                                 //如果不存在,则废除,哪个队列都不加入
  124.                                 expiredList.addElement(t);
  125.                                 found++;
  126.                                 //如果持续的话,则继续把该计时器再次加入计时器队列中
  127.                                 if (t.ispermanent() && !shuttingdown) {
  128.                                     t.restart();
  129.                                     timerList.addElement(t);
  130.                                 }
  131.                             }
  132.                         }
  133.                         // Be sure there is no more ref on bean in this local variable.
  134.                         t = null;
  135.                     }
  136.                     if (found > 0) {
  137.                         //唤醒线程;
  138.                         timerList.notify();
  139.                     } else {
  140.                         if (empty && shuttingdown) {
  141.                             break;
  142.                         }
  143.                     }
  144.                 }
  145.             } catch (InterruptedException e) {
  146.                 TraceTm.jta.error("Timer interrupted");
  147.             }
  148.         }
  149.         synchronized(timerList) { // notify batch so that function can return.
  150.             timerList.notify();
  151.         }
  152.     }
  153.     /**
  154.      * process all expired timers
  155.      */
  156.     public void batch() {
  157.         while (!(shuttingdown && timerList.isEmpty() && expiredList.isEmpty())) {
  158.             TimerEvent t;
  159.             synchronized(timerList) {
  160.                 while (expiredList.isEmpty()) {
  161.                     if (shuttingdown) return;
  162.                     try {
  163.                         //计时器计时线程让如果找到到期的计时器,那么就会唤醒执行该计时器的线程执行监听器动作
  164.                         timerList.wait();
  165.                     } catch (Exception e) {
  166.                         TraceTm.jta.error("Exception in Batch: ", e);
  167.                     }
  168.                 }
  169.                 t = (TimerEvent) expiredList.elementAt(0);
  170.                 expiredList.removeElementAt(0);
  171.             }
  172.             //执行动作;
  173.             t.process();
  174.         }
  175.     }
  176.     /**
  177.      * add a new timer in the list
  178.      * @param tel Object that will be notified when the timer expire.
  179.      * @param timeout nb of seconds before the timer expires.
  180.      * @param arg info passed with the timer
  181.      * @param permanent true if the timer is permanent.
  182.      */
  183.     public TimerEvent addTimer(TimerEventListener tel, long timeout, Object arg, boolean permanent) {
  184.         TimerEvent te = new TimerEvent(tel, timeout, arg, permanent);
  185.         synchronized(timerList) {
  186.             timerList.addElement(te);
  187.         }
  188.         return te;
  189.     }
  190.     /**
  191.      * remove a timer from the list. this is not very efficient.
  192.      * A better way to do this is TimerEvent.unset()
  193.      * @deprecated
  194.      */
  195.     public void removeTimer(TimerEvent te) {
  196.         synchronized(timerList) {
  197.             timerList.removeElement(te);
  198.         }
  199.     }
  200. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值