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

相关文章

慢sql提前分析预警和动态sql替换-Mybatis-SQL

《慢sql提前分析预警和动态sql替换-Mybatis-SQL》为防止慢SQL问题而开发的MyBatis组件,该组件能够在开发、测试阶段自动分析SQL语句,并在出现慢SQL问题时通过Ducc配置实现动... 目录背景解决思路开源方案调研设计方案详细设计使用方法1、引入依赖jar包2、配置组件XML3、核心配

Java NoClassDefFoundError运行时错误分析解决

《JavaNoClassDefFoundError运行时错误分析解决》在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能... 目录前言一、问题分析二、报错原因三、解决思路检查类路径配置检查依赖库检查类文件调试类加载器问题四、常见

Python中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑

Java程序进程起来了但是不打印日志的原因分析

《Java程序进程起来了但是不打印日志的原因分析》:本文主要介绍Java程序进程起来了但是不打印日志的原因分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java程序进程起来了但是不打印日志的原因1、日志配置问题2、日志文件权限问题3、日志文件路径问题4、程序

使用Python实现一个优雅的异步定时器

《使用Python实现一个优雅的异步定时器》在Python中实现定时器功能是一个常见需求,尤其是在需要周期性执行任务的场景下,本文给大家介绍了基于asyncio和threading模块,可扩展的异步定... 目录需求背景代码1. 单例事件循环的实现2. 事件循环的运行与关闭3. 定时器核心逻辑4. 启动与停

Java 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思

Java字符串操作技巧之语法、示例与应用场景分析

《Java字符串操作技巧之语法、示例与应用场景分析》在Java算法题和日常开发中,字符串处理是必备的核心技能,本文全面梳理Java中字符串的常用操作语法,结合代码示例、应用场景和避坑指南,可快速掌握字... 目录引言1. 基础操作1.1 创建字符串1.2 获取长度1.3 访问字符2. 字符串处理2.1 子字

Python 迭代器和生成器概念及场景分析

《Python迭代器和生成器概念及场景分析》yield是Python中实现惰性计算和协程的核心工具,结合send()、throw()、close()等方法,能够构建高效、灵活的数据流和控制流模型,这... 目录迭代器的介绍自定义迭代器省略的迭代器生产器的介绍yield的普通用法yield的高级用法yidle

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++