1 22 package org.jboss.util; 23 24 35 public abstract class TimerTask 36 implements Executable, Comparable 37 { 38 39 static final int NEW = 1; 40 41 static final int SCHEDULED = 2; 42 43 static final int EXECUTED = 3; 44 45 static final int CANCELLED = 4; 46 47 private final Object m_lock = new Object (); 49 private int m_state; 50 private final long m_period; 52 private long m_nextExecutionTime; 53 54 57 protected TimerTask() 58 { 59 m_state = NEW; 60 m_period = 0; 61 } 62 63 68 protected TimerTask(long period) 69 { 70 m_state = NEW; 71 if (period < 0) throw new IllegalArgumentException ("Period can't be negative"); 72 m_period = period; 73 } 74 75 81 public boolean cancel() 82 { 83 synchronized (getLock()) 84 { 85 boolean ret = (m_state == SCHEDULED); 86 m_state = CANCELLED; 87 return ret; 88 } 89 } 90 91 93 96 public abstract void execute() throws Exception ; 97 98 100 104 public int compareTo(Object other) 105 { 106 if (other == this) return 0; 107 TimerTask t = (TimerTask) other; 108 long diff = getNextExecutionTime() - t.getNextExecutionTime(); 109 return (int) diff; 110 } 111 112 113 Object getLock() 114 { 115 return m_lock; 116 } 117 118 119 void setState(int state) 120 { 121 synchronized (getLock()) 122 { 123 m_state = state; 124 } 125 } 126 127 128 int getState() 129 { 130 synchronized (getLock()) 131 { 132 return m_state; 133 } 134 } 135 136 137 boolean isPeriodic() 138 { 139 return m_period > 0; 140 } 141 142 143 long getNextExecutionTime() 144 { 145 synchronized (getLock()) 146 { 147 return m_nextExecutionTime; 148 } 149 } 150 151 152 void setNextExecutionTime(long time) 153 { 154 synchronized (getLock()) 155 { 156 m_nextExecutionTime = time; 157 } 158 } 159 160 161 protected long getPeriod() 162 { 163 return m_period; 164 } 165 } 166 | Popular Tags |