public void clock() {
//无限循环
while (true) {
try {
//线程休息一秒
Thread.sleep(shuttingdown?1:1000); // 1 second or 1ms shen shuttingdown
// Thread.currentThread().sleep(shuttingdown?1:1000); // 1 second or 1ms shen shuttingdown
synchronized(timerList) {
int found = 0;
boolean empty = true;
for (int i = 0; i < timerList.size(); i++) {
TimerEvent t = (TimerEvent) timerList.elementAt(i);
//如果没有活动的计时器,那么计时器队列为空;
if (!t.isStopped()) {
empty = false;
}
//如果计时器过期
if (t.update() <= 0) {
//从计时器队列中移除
timerList.removeElementAt(i--);
if (t.valid()) {
//该计时器存在计时器监听器的话,把这个计时器加入过期计时器
//如果不存在,则废除,哪个队列都不加入
expiredList.addElement(t);
found++;
//如果持续的话,则继续把该计时器再次加入计时器队列中
if (t.ispermanent() && !shuttingdown) {
t.restart();
timerList.addElement(t);
}
}
}
// Be sure there is no more ref on bean in this local variable.
t = null;
}
if (found > 0) {
//唤醒线程;
timerList.notify();
} else {
if (empty && shuttingdown) {
break;
}
}
}
} catch (InterruptedException e) {
TraceTm.jta.error("Timer interrupted");
}
}
synchronized(timerList) { // notify batch so that function can return.
timerList.notify();
}
}
/**
* process all expired timers
*/
public void batch() {
while (!(shuttingdown && timerList.isEmpty() && expiredList.isEmpty())) {
TimerEvent t;
synchronized(timerList) {
while (expiredList.isEmpty()) {
if (shuttingdown) return;
try {
//计时器计时线程让如果找到到期的计时器,那么就会唤醒执行该计时器的线程执行监听器动作
timerList.wait();
} catch (Exception e) {
TraceTm.jta.error("Exception in Batch: ", e);
}
}
t = (TimerEvent) expiredList.elementAt(0);
expiredList.removeElementAt(0);
}
//执行动作;
t.process();
}
}
/**
* add a new timer in the list
* @param tel Object that will be notified when the timer expire.
* @param timeout nb of seconds before the timer expires.
* @param arg info passed with the timer
* @param permanent true if the timer is permanent.
*/
public TimerEvent addTimer(TimerEventListener tel, long timeout, Object arg, boolean permanent) {
TimerEvent te = new TimerEvent(tel, timeout, arg, permanent);
synchronized(timerList) {
timerList.addElement(te);
}
return te;
}
/**
* remove a timer from the list. this is not very efficient.
* A better way to do this is TimerEvent.unset()
* @deprecated
*/
public void removeTimer(TimerEvent te) {
synchronized(timerList) {
timerList.removeElement(te);
}
}
}
上一页 [1] [2]
责任编辑:小草