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

相关文章

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

C#使用DeepSeek API实现自然语言处理,文本分类和情感分析

《C#使用DeepSeekAPI实现自然语言处理,文本分类和情感分析》在C#中使用DeepSeekAPI可以实现多种功能,例如自然语言处理、文本分类、情感分析等,本文主要为大家介绍了具体实现步骤,... 目录准备工作文本生成文本分类问答系统代码生成翻译功能文本摘要文本校对图像描述生成总结在C#中使用Deep

Go中sync.Once源码的深度讲解

《Go中sync.Once源码的深度讲解》sync.Once是Go语言标准库中的一个同步原语,用于确保某个操作只执行一次,本文将从源码出发为大家详细介绍一下sync.Once的具体使用,x希望对大家有... 目录概念简单示例源码解读总结概念sync.Once是Go语言标准库中的一个同步原语,用于确保某个操

Redis主从/哨兵机制原理分析

《Redis主从/哨兵机制原理分析》本文介绍了Redis的主从复制和哨兵机制,主从复制实现了数据的热备份和负载均衡,而哨兵机制可以监控Redis集群,实现自动故障转移,哨兵机制通过监控、下线、选举和故... 目录一、主从复制1.1 什么是主从复制1.2 主从复制的作用1.3 主从复制原理1.3.1 全量复制

Redis主从复制的原理分析

《Redis主从复制的原理分析》Redis主从复制通过将数据镜像到多个从节点,实现高可用性和扩展性,主从复制包括初次全量同步和增量同步两个阶段,为优化复制性能,可以采用AOF持久化、调整复制超时时间、... 目录Redis主从复制的原理主从复制概述配置主从复制数据同步过程复制一致性与延迟故障转移机制监控与维

Redis连接失败:客户端IP不在白名单中的问题分析与解决方案

《Redis连接失败:客户端IP不在白名单中的问题分析与解决方案》在现代分布式系统中,Redis作为一种高性能的内存数据库,被广泛应用于缓存、消息队列、会话存储等场景,然而,在实际使用过程中,我们可能... 目录一、问题背景二、错误分析1. 错误信息解读2. 根本原因三、解决方案1. 将客户端IP添加到Re

Java汇编源码如何查看环境搭建

《Java汇编源码如何查看环境搭建》:本文主要介绍如何在IntelliJIDEA开发环境中搭建字节码和汇编环境,以便更好地进行代码调优和JVM学习,首先,介绍了如何配置IntelliJIDEA以方... 目录一、简介二、在IDEA开发环境中搭建汇编环境2.1 在IDEA中搭建字节码查看环境2.1.1 搭建步

Redis主从复制实现原理分析

《Redis主从复制实现原理分析》Redis主从复制通过Sync和CommandPropagate阶段实现数据同步,2.8版本后引入Psync指令,根据复制偏移量进行全量或部分同步,优化了数据传输效率... 目录Redis主DodMIK从复制实现原理实现原理Psync: 2.8版本后总结Redis主从复制实

锐捷和腾达哪个好? 两个品牌路由器对比分析

《锐捷和腾达哪个好?两个品牌路由器对比分析》在选择路由器时,Tenda和锐捷都是备受关注的品牌,各自有独特的产品特点和市场定位,选择哪个品牌的路由器更合适,实际上取决于你的具体需求和使用场景,我们从... 在选购路由器时,锐捷和腾达都是市场上备受关注的品牌,但它们的定位和特点却有所不同。锐捷更偏向企业级和专