timer.java

来自「大量java源程序」· Java 代码 · 共 93 行

JAVA
93
字号
/* * @(#)Timer * * Copyright (c) 1998 Karl Moss. All Rights Reserved. * * You may study, use, modify, and distribute this software for any * purpose provided that this copyright notice appears in all copies. * * This software is provided WITHOUT WARRANTY either expressed or * implied. * * @author  Karl Moss * @version 1.0 * @date    11Mar98 * */package javaservlets.timer;/**  * <p>This class implements a simple timer. Every time the  * timer clock cycle that is specified expires the TimerEvent  * method on the given TimerListener object will be invoked.  * This gives the object a chance to perform some type of  * timeout checking.  */public class Timer extends Thread{  // TimerListener to receive TimerEvent notifications  TimerListener m_timerListener;  // Number of seconds in each timer cycle  int m_cycle;  // Object to be supplied with the TimerEvent notification  Object m_object;  /**    * <p>Constructs a new Timer object    *    * @param timerListener Object that will receive TimerEvent    * notifications    * @param cycle Number of seconds in each timer cycle    */  public Timer(TimerListener timerListener, int cycle)    {      m_timerListener = timerListener;      m_cycle = cycle;      m_object = null;    }  /**    * <p>Constructs a new Timer object    *    * @param timerListener Object that will receive TimerEvent    * notifications    * @param cycle Number of seconds in each timer cycle    * @param object Object to be supplied with the TimerEvent    * notification    */  public Timer(TimerListener timerListener, int cycle,               Object object)    {      m_timerListener = timerListener;      m_cycle = cycle;      m_object = object;    }  /**    * <p>Runs the timer. The timer will run until stopped and    * fire a TimerEvent notification every clock cycle    */  public void run()    {      // Loop until stopped      while (true) {        try {          // Sleep for the clock cycle          sleep(m_cycle * 1000);        }        catch (InterruptedException ex) {        }        // Fire a TimerEvent        if (m_timerListener != null) {          m_timerListener.TimerEvent(m_object);        }      }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?