JAVA定时器

本文转自http://blog.csdn.net/cping1982/archive/2007/11/21/1896794.aspx 

用JAVA写了一个定时器类,此类作为一个线程运行,包含了一个队列存放定时器消息,通过比较定时器的触发时间,俺触发事件先后顺序排列队列。

import java.util.concurrent.*;
import java.util.*;

public class TimerTask implements Runnable ...{
    /** *//**定时器事件队列*/
    PriorityBlockingQueue<TimerSlot> queue_ =  new PriorityBlockingQueue<TimerSlot>(2048);
    /** *//** Creates a new instance of TimerTask */
    public TimerTask() ...{
    }
   
    /** *//**设置定时器,并放入队列*/
    public synchronized Object setTimer(String taskname, String eventname, int timerevent, int delay, Object param)...{
        TimerSlot slot = new TimerSlot();
        slot.taskname = taskname;
        slot.eventname = eventname;
        slot.timerevent = timerevent;
        slot.expiretime = System.currentTimeMillis() + delay;
        slot.param = param;
        try ...{
            queue_.put(slot);
        } catch (Exception ex) ...{
        }
        return 0;
    }
   
    /** *//**去掉指定的定时器*/
    protected synchronized int killTimer(Object timer)...{
        if (queue_.remove(timer))...{
            return 0;
        }
        return -1;
    }
   
    /** *//**关闭操作*/
    public void close()...{
        queue_.clear();
    }
   
    /** *//**线程运行,检查定时器是否触发*/
    public void run()...{
        while (true)...{
            try...{
                TimerSlot slot = queue_.peek();
                    if (slot == null) ...{
                        Thread.sleep(50);
                        continue;
                    }
                    long now = System.currentTimeMillis();
                    if (now < slot.expiretime) ...{
                        Thread.sleep(Math.min(slot.expiretime-now,50));
                        continue;
                    }
                    queue_.remove();
                   
                    TaskMsg taskmsg = new TaskMsg();
                    taskmsg.eventName=slot.eventname;
                    taskmsg.eventNo = slot.timerevent;
                    taskmsg.msg = (slot.param);
                    taskmsg.sender = "__timer";
                    taskmsg.receiver = slot.taskname;
                    TaskFactory.getInstance().getTaskByName(slot.taskname).
                            putmsgtoqueue(taskmsg);
            }catch(InterruptedException e)...{
                return;
            }
        }
    }
   
    class TimerSlot implements java.lang.Comparable<TimerSlot>...{
        public String eventname;
        public String taskname;
        public int timerevent;
        public long expiretime; // ms, System.current+delay
        public Object param;
        public int compareTo(TimerSlot  other) ...{
            return (int)(this.expiretime-other.expiretime);
        }
    }
}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/njchenyi/archive/2007/12/03/1913073.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值