001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.util;
019:
020: /**
021: * The TimerTask class is represents a task to run at specified time. The task
022: * may be run once or repeatedly.
023: *
024: * @see Timer
025: * @see java.lang.Object#wait(long)
026: */
027: public abstract class TimerTask implements Runnable {
028: /* Lock object for synchronization. It's also used by Timer class. */
029: final Object lock = new Object();
030:
031: /* If timer was cancelled */
032: boolean cancelled;
033:
034: /* Slots used by Timer */
035: long when;
036:
037: long period;
038:
039: boolean fixedRate;
040:
041: /*
042: * The time when task will be executed, or the time when task was launched
043: * if this is task in progress.
044: */
045: private long scheduledTime;
046:
047: /*
048: * Method called from the Timer for synchronized getting of when field.
049: */
050: long getWhen() {
051: synchronized (lock) {
052: return when;
053: }
054: }
055:
056: /*
057: * Method called from the Timer object when scheduling an event @param time
058: */
059: void setScheduledTime(long time) {
060: synchronized (lock) {
061: scheduledTime = time;
062: }
063: }
064:
065: /*
066: * Is TimerTask scheduled into any timer?
067: *
068: * @return <code>true</code> if the timer task is scheduled, <code>false</code>
069: * otherwise.
070: */
071: boolean isScheduled() {
072: synchronized (lock) {
073: return when > 0 || scheduledTime > 0;
074: }
075: }
076:
077: protected TimerTask() {
078: super ();
079: }
080:
081: /**
082: * Cancels the Task and removes it from the Timer's queue. Generally, it
083: * returns false if the call did not prevent a TimerTask from running at
084: * least once. Subsequent calls have no effect.
085: *
086: * @return <code>true</code> if the call prevented a scheduled execution
087: * from taking place, <code>false</code> otherwise.
088: */
089: public boolean cancel() {
090: synchronized (lock) {
091: boolean willRun = !cancelled && when > 0;
092: cancelled = true;
093: return willRun;
094: }
095: }
096:
097: /**
098: * Returns the scheduled execution time. If the task execution is in
099: * progress returns the execution time of ongoing task. Tasks which have not
100: * yet run return an undefined value.
101: *
102: * @return the most recent execution time.
103: */
104: public long scheduledExecutionTime() {
105: synchronized (lock) {
106: return scheduledTime;
107: }
108: }
109:
110: /**
111: * The task to run should be specified in the implementation of the run()
112: * method.
113: */
114: public abstract void run();
115:
116: }
|