|
1 /* |
|
2 * Licensed to the Apache Software Foundation (ASF) under one or more |
|
3 * contributor license agreements. See the NOTICE file distributed with |
|
4 * this work for additional information regarding copyright ownership. |
|
5 * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
6 * (the "License"); you may not use this file except in compliance with |
|
7 * the License. You may obtain a copy of the License at |
|
8 * |
|
9 * http://www.apache.org/licenses/LICENSE-2.0 |
|
10 * |
|
11 * Unless required by applicable law or agreed to in writing, software |
|
12 * distributed under the License is distributed on an "AS IS" BASIS, |
|
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 * See the License for the specific language governing permissions and |
|
15 * limitations under the License. |
|
16 */ |
|
17 |
|
18 /** |
|
19 * TimerTask implementation adapted to Java ME from Apache Harmony (Open Source Java SE) |
|
20 * Main changes: |
|
21 * - none |
|
22 */ |
|
23 |
|
24 package java.util; |
|
25 |
|
26 /** |
|
27 * The {@code TimerTask} class represents a task to run at a specified time. The task |
|
28 * may be run once or repeatedly. |
|
29 * |
|
30 * @see Timer |
|
31 * @see java.lang.Object#wait(long) |
|
32 */ |
|
33 public abstract class TimerTask implements Runnable |
|
34 { |
|
35 /* Lock object for synchronization. It's also used by Timer class. */ |
|
36 final Object lock = new Object(); |
|
37 |
|
38 /* If timer was cancelled */ |
|
39 boolean cancelled; |
|
40 |
|
41 /* Slots used by Timer */ |
|
42 long when; |
|
43 |
|
44 long period; |
|
45 |
|
46 boolean fixedRate; |
|
47 |
|
48 /* |
|
49 * The time when task will be executed, or the time when task was launched |
|
50 * if this is task in progress. |
|
51 */ |
|
52 private long scheduledTime; |
|
53 |
|
54 /* |
|
55 * Method called from the Timer for synchronized getting of when field. |
|
56 */ |
|
57 long getWhen() |
|
58 { |
|
59 synchronized (lock) |
|
60 { |
|
61 return when; |
|
62 } |
|
63 } |
|
64 |
|
65 /* |
|
66 * Method called from the Timer object when scheduling an event @param time |
|
67 */ |
|
68 void setScheduledTime(long time) |
|
69 { |
|
70 synchronized (lock) |
|
71 { |
|
72 scheduledTime = time; |
|
73 } |
|
74 } |
|
75 |
|
76 /* |
|
77 * Is TimerTask scheduled into any timer? |
|
78 * |
|
79 * @return {@code true} if the timer task is scheduled, {@code false} |
|
80 * otherwise. |
|
81 */ |
|
82 boolean isScheduled() |
|
83 { |
|
84 synchronized (lock) |
|
85 { |
|
86 return when > 0 || scheduledTime > 0; |
|
87 } |
|
88 } |
|
89 |
|
90 /** |
|
91 * Creates a new {@code TimerTask}. |
|
92 */ |
|
93 protected TimerTask() |
|
94 { |
|
95 super(); |
|
96 } |
|
97 |
|
98 /** |
|
99 * Cancels the {@code TimerTask} and removes it from the {@code Timer}'s queue. Generally, it |
|
100 * returns {@code false} if the call did not prevent a {@code TimerTask} from running at |
|
101 * least once. Subsequent calls have no effect. |
|
102 * |
|
103 * @return {@code true} if the call prevented a scheduled execution |
|
104 * from taking place, {@code false} otherwise. |
|
105 */ |
|
106 public boolean cancel() |
|
107 { |
|
108 synchronized (lock) |
|
109 { |
|
110 boolean willRun = !cancelled && when > 0; |
|
111 cancelled = true; |
|
112 return willRun; |
|
113 } |
|
114 } |
|
115 |
|
116 /** |
|
117 * Returns the scheduled execution time. If the task execution is in |
|
118 * progress it returns the execution time of the ongoing task. Tasks which |
|
119 * have not yet run return an undefined value. |
|
120 * |
|
121 * @return the most recent execution time. |
|
122 */ |
|
123 public long scheduledExecutionTime() |
|
124 { |
|
125 synchronized (lock) |
|
126 { |
|
127 return scheduledTime; |
|
128 } |
|
129 } |
|
130 |
|
131 /** |
|
132 * The task to run should be specified in the implementation of the {@code run()} |
|
133 * method. |
|
134 */ |
|
135 public abstract void run(); |
|
136 |
|
137 } |