01: /* ScalableTimerTask.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Wed Dec 5 14:31:40 2007, Created by tomyeh
10: }}IS_NOTE
11:
12: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
13:
14: {{IS_RIGHT
15: This program is distributed under GPL Version 2.0 in the hope that
16: it will be useful, but WITHOUT ANY WARRANTY.
17: }}IS_RIGHT
18: */
19: package org.zkoss.util;
20:
21: import java.util.TimerTask;
22:
23: /**
24: * A task that can be scheduled for one-time execution by
25: * a scalable timer ({@link ScalableTimer}.
26: *
27: * @author tomyeh
28: * @since 3.0.1
29: */
30: abstract public class ScalableTimerTask extends TimerTask {
31: private ScalableTimerInfo _ti;
32:
33: /*package*/void setScalableTimerInfo(ScalableTimerInfo ti) {
34: _ti = ti;
35: }
36:
37: /** The action to be performed by this timer task.
38: * The derived class must override this method instead of {@link #run}.
39: */
40: abstract public void exec();
41:
42: //super//
43: /** Invokes {@link #exec}.
44: * The derived class shall not override this method.
45: * Rather, override {@link #exec} instead.
46: */
47: final public void run() {
48: setDone();
49: exec();
50: }
51:
52: /** Cancels this timer task.
53: *
54: * @return true if this task is scheduled for one-time execution and has not yet run.
55: * Returns false if the task was scheduled for one-time execution and has already run.
56: */
57: public boolean cancel() {
58: final boolean b = super .cancel();
59: if (b)
60: setDone();
61: return b;
62: }
63:
64: private void setDone() {
65: if (_ti != null) {
66: synchronized (_ti) {
67: --_ti.count;
68: }
69: _ti = null;
70: }
71: }
72: }
|