KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > kjoram > util > TimerTask


1 /*
2  * Copyright (C) 2002 - ScalAgent Distributed Technologies
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA.
18  *
19  * The present code contributor is ScalAgent Distributed Technologies.
20  */

21 package com.scalagent.kjoram.util;
22
23 /**
24  * The <code>TimerTask</code> class is the base class for implementing
25  * tasks run by a <code>Timer</code> instance.
26  *
27  * @see Timer
28  */

29 public abstract class TimerTask
30 {
31   /** The task's timer reference. */
32   Timer timer;
33   /** The task's wake up time. */
34   long wakeupTime;
35   /** <code>true</code> if the task is the next scheduled. */
36   boolean waiting = false;
37   /** <code>true</code> if the task has been cancelled. */
38   boolean cancelled = false;
39
40   /**
41    * Cancels this task by removing it from the timer's tasks list, and
42    * interrupting the timer thread if necessary.
43    */

44   public boolean cancel()
45   {
46     synchronized (timer) {
47       cancelled = true;
48
49       if (timer.tasks.contains(this)) {
50         timer.tasks.removeElement(this);
51
52         if (waiting)
53           timer.daemon.interrupt();
54
55         return true;
56       }
57       return false;
58     }
59   }
60
61   /** Method called by the timer when the task is ready to execute. */
62   public abstract void run();
63 }
64
Popular Tags