KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > varscheduler > SchedulerTask


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 package org.apache.servicemix.components.varscheduler;
18
19 import java.util.*;
20
21 /**
22  * A task run by a {@link Scheduler}.
23  *
24  * @author George Gastaldi (gastaldi)
25  */

26 public abstract class SchedulerTask implements Runnable JavaDoc {
27
28     static final int VIRGIN = 0;
29     static final int SCHEDULED = 1;
30     static final int CANCELLED = 2;
31
32     final Object JavaDoc lock = new Object JavaDoc();
33     int state = VIRGIN;
34     TimerTask timerTask;
35
36     protected SchedulerTask() {
37     }
38
39     public abstract void run();
40
41     /**
42      * Cancels task.
43      * @return true if task already scheduled
44      */

45     public boolean cancel() {
46         synchronized (lock) {
47             if (timerTask != null) {
48                 timerTask.cancel();
49             }
50             boolean result = (state == SCHEDULED);
51             state = CANCELLED;
52             return result;
53         }
54     }
55
56     public long scheduledExecutionTime() {
57         synchronized (lock) {
58             return timerTask == null ? 0 : timerTask.scheduledExecutionTime();
59         }
60     }
61
62 }
63
64
Popular Tags