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

相关文章

kotlin中const 和val的区别及使用场景分析

《kotlin中const和val的区别及使用场景分析》在Kotlin中,const和val都是用来声明常量的,但它们的使用场景和功能有所不同,下面给大家介绍kotlin中const和val的区别,... 目录kotlin中const 和val的区别1. val:2. const:二 代码示例1 Java

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

Spring事务中@Transactional注解不生效的原因分析与解决

《Spring事务中@Transactional注解不生效的原因分析与解决》在Spring框架中,@Transactional注解是管理数据库事务的核心方式,本文将深入分析事务自调用的底层原理,解释为... 目录1. 引言2. 事务自调用问题重现2.1 示例代码2.2 问题现象3. 为什么事务自调用会失效3

找不到Anaconda prompt终端的原因分析及解决方案

《找不到Anacondaprompt终端的原因分析及解决方案》因为anaconda还没有初始化,在安装anaconda的过程中,有一行是否要添加anaconda到菜单目录中,由于没有勾选,导致没有菜... 目录问题原因问http://www.chinasem.cn题解决安装了 Anaconda 却找不到 An

Spring定时任务只执行一次的原因分析与解决方案

《Spring定时任务只执行一次的原因分析与解决方案》在使用Spring的@Scheduled定时任务时,你是否遇到过任务只执行一次,后续不再触发的情况?这种情况可能由多种原因导致,如未启用调度、线程... 目录1. 问题背景2. Spring定时任务的基本用法3. 为什么定时任务只执行一次?3.1 未启用

C++ 各种map特点对比分析

《C++各种map特点对比分析》文章比较了C++中不同类型的map(如std::map,std::unordered_map,std::multimap,std::unordered_multima... 目录特点比较C++ 示例代码 ​​​​​​代码解释特点比较1. std::map底层实现:基于红黑

Springboot如何配置Scheduler定时器

《Springboot如何配置Scheduler定时器》:本文主要介绍Springboot如何配置Scheduler定时器问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录Springboot配置Scheduler定时器1.在启动类上添加 @EnableSchedulin

Spring、Spring Boot、Spring Cloud 的区别与联系分析

《Spring、SpringBoot、SpringCloud的区别与联系分析》Spring、SpringBoot和SpringCloud是Java开发中常用的框架,分别针对企业级应用开发、快速开... 目录1. Spring 框架2. Spring Boot3. Spring Cloud总结1. Sprin

Spring 中 BeanFactoryPostProcessor 的作用和示例源码分析

《Spring中BeanFactoryPostProcessor的作用和示例源码分析》Spring的BeanFactoryPostProcessor是容器初始化的扩展接口,允许在Bean实例化前... 目录一、概览1. 核心定位2. 核心功能详解3. 关键特性二、Spring 内置的 BeanFactory