JOTM中定时器的源码分析

2024-04-27 11:18
文章标签 分析 源码 定时器 jotm

本文主要是介绍JOTM中定时器的源码分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在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. }

这篇关于JOTM中定时器的源码分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/940363

相关文章

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Python主动抛出异常的各种用法和场景分析

《Python主动抛出异常的各种用法和场景分析》在Python中,我们不仅可以捕获和处理异常,还可以主动抛出异常,也就是以类的方式自定义错误的类型和提示信息,这在编程中非常有用,下面我将详细解释主动抛... 目录一、为什么要主动抛出异常?二、基本语法:raise关键字基本示例三、raise的多种用法1. 抛

github打不开的问题分析及解决

《github打不开的问题分析及解决》:本文主要介绍github打不开的问题分析及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、找到github.com域名解析的ip地址二、找到github.global.ssl.fastly.net网址解析的ip地址三

Mysql的主从同步/复制的原理分析

《Mysql的主从同步/复制的原理分析》:本文主要介绍Mysql的主从同步/复制的原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录为什么要主从同步?mysql主从同步架构有哪些?Mysql主从复制的原理/整体流程级联复制架构为什么好?Mysql主从复制注意

java -jar命令运行 jar包时运行外部依赖jar包的场景分析

《java-jar命令运行jar包时运行外部依赖jar包的场景分析》:本文主要介绍java-jar命令运行jar包时运行外部依赖jar包的场景分析,本文给大家介绍的非常详细,对大家的学习或工作... 目录Java -jar命令运行 jar包时如何运行外部依赖jar包场景:解决:方法一、启动参数添加: -Xb

Apache 高级配置实战之从连接保持到日志分析的完整指南

《Apache高级配置实战之从连接保持到日志分析的完整指南》本文带你从连接保持优化开始,一路走到访问控制和日志管理,最后用AWStats来分析网站数据,对Apache配置日志分析相关知识感兴趣的朋友... 目录Apache 高级配置实战:从连接保持到日志分析的完整指南前言 一、Apache 连接保持 - 性

Linux中的more 和 less区别对比分析

《Linux中的more和less区别对比分析》在Linux/Unix系统中,more和less都是用于分页查看文本文件的命令,但less是more的增强版,功能更强大,:本文主要介绍Linu... 目录1. 基础功能对比2. 常用操作对比less 的操作3. 实际使用示例4. 为什么推荐 less?5.

spring-gateway filters添加自定义过滤器实现流程分析(可插拔)

《spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔)》:本文主要介绍spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔),本文通过实例图... 目录需求背景需求拆解设计流程及作用域逻辑处理代码逻辑需求背景公司要求,通过公司网络代理访问的请求需要做请

Java集成Onlyoffice的示例代码及场景分析

《Java集成Onlyoffice的示例代码及场景分析》:本文主要介绍Java集成Onlyoffice的示例代码及场景分析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 需求场景:实现文档的在线编辑,团队协作总结:两个接口 + 前端页面 + 配置项接口1:一个接口,将o

IDEA下"File is read-only"可能原因分析及"找不到或无法加载主类"的问题

《IDEA下Fileisread-only可能原因分析及找不到或无法加载主类的问题》:本文主要介绍IDEA下Fileisread-only可能原因分析及找不到或无法加载主类的问题,具有很好的参... 目录1.File is read-only”可能原因2.“找不到或无法加载主类”问题的解决总结1.File