KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > TimerTask


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util;
23
24 /**
25  * A class that represent a task that can be scheduled for one-shot or
26  * repeated execution by a {@link TimerQueue}. <p>
27  * A similar class is present in java.util package of jdk version >= 1.3;
28  * for compatibility with jdk 1.2 we reimplemented it.
29  *
30  * @see TimerQueue
31  *
32  * @author <a HREF="mailto:simone.bordet@compaq.com">Simone Bordet</a>
33  * @version $Revision: 1958 $
34  */

35 public abstract class TimerTask
36    implements Executable, Comparable JavaDoc
37 {
38    /** The state before the first execution */
39    static final int NEW = 1;
40    /** The state after first execution if the TimerTask is repeating */
41    static final int SCHEDULED = 2;
42    /** The state after first execution if the TimerTask is not repeating */
43    static final int EXECUTED = 3;
44    /** The state when cancelled */
45    static final int CANCELLED = 4;
46
47    // Attributes ----------------------------------------------------
48
private final Object JavaDoc m_lock = new Object JavaDoc();
49    private int m_state;
50    // this is a constant, and need not be locked
51
private final long m_period;
52    private long m_nextExecutionTime;
53
54    /**
55     * Creates a TimerTask object that will be executed once.
56     */

57    protected TimerTask()
58    {
59       m_state = NEW;
60       m_period = 0;
61    }
62    
63    /**
64     * Creates a TimerTask object that will be executed every <code>period</code>
65     * milliseconds. <p>
66     * @param period the execution period; if zero, will be executed only once.
67     */

68    protected TimerTask(long period)
69    {
70       m_state = NEW;
71       if (period < 0) throw new IllegalArgumentException JavaDoc("Period can't be negative");
72       m_period = period;
73    }
74
75    /**
76     * Cancels the next execution of this TimerTask (if any). <br>
77     * If the TimerTask is executing it will prevent the next execution (if any).
78     * @returns true if one or more scheduled execution will not take place,
79     * false otherwise.
80     */

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    // Executable implementation ---------------------------------------
92

93    /**
94     * The task to be executed, to be implemented in subclasses.
95     */

96    public abstract void execute() throws Exception JavaDoc;
97
98    // Comparable implementation ---------------------------------------
99

100    /**
101     * A TimerTask is less than another if it will be scheduled before.
102     * @throws ClassCastException if other is not a TimerTask, according to the Comparable contract
103     */

104    public int compareTo(Object JavaDoc 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    /** Returns the mutex that syncs the access to this object */
113    Object JavaDoc getLock()
114    {
115       return m_lock;
116    }
117    
118    /** Sets the state of execution of this TimerTask */
119    void setState(int state)
120    {
121       synchronized (getLock())
122       {
123          m_state = state;
124       }
125    }
126    
127    /** Returns the state of execution of this TimerTask */
128    int getState()
129    {
130       synchronized (getLock())
131       {
132          return m_state;
133       }
134    }
135    
136    /** Returns whether this TimerTask is periodic */
137    boolean isPeriodic()
138    {
139       return m_period > 0;
140    }
141    
142    /** Returns the next execution time for this TimerTask */
143    long getNextExecutionTime()
144    {
145       synchronized (getLock())
146       {
147          return m_nextExecutionTime;
148       }
149    }
150    
151    /** Sets the next execution time for this TimerTask */
152    void setNextExecutionTime(long time)
153    {
154       synchronized (getLock())
155       {
156          m_nextExecutionTime = time;
157       }
158    }
159
160    /** Returns the period of this TimerTask */
161    protected long getPeriod()
162    {
163       return m_period;
164    }
165 }
166
Popular Tags