View Javadoc
1   //******************************************************************************
2   //
3   // File:    TimerTask.java
4   // Package: edu.rit.util
5   // Unit:    Interface edu.rit.util.TimerTask
6   //
7   // This Java source file is copyright (C) 2002-2004 by Alan Kaminsky. All rights
8   // reserved. For further information, contact the author, Alan Kaminsky, at
9   // ark@cs.rit.edu.
10  //
11  // This Java source file is part of the Parallel Java Library ("PJ"). PJ is free
12  // software; you can redistribute it and/or modify it under the terms of the GNU
13  // General Public License as published by the Free Software Foundation; either
14  // version 3 of the License, or (at your option) any later version.
15  //
16  // PJ is distributed in the hope that it will be useful, but WITHOUT ANY
17  // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18  // A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19  //
20  // Linking this library statically or dynamically with other modules is making a
21  // combined work based on this library. Thus, the terms and conditions of the GNU
22  // General Public License cover the whole combination.
23  //
24  // As a special exception, the copyright holders of this library give you
25  // permission to link this library with independent modules to produce an
26  // executable, regardless of the license terms of these independent modules, and
27  // to copy and distribute the resulting executable under terms of your choice,
28  // provided that you also meet, for each linked independent module, the terms
29  // and conditions of the license of that module. An independent module is a module
30  // which is not derived from or based on this library. If you modify this library,
31  // you may extend this exception to your version of the library, but you are not
32  // obligated to do so. If you do not wish to do so, delete this exception
33  // statement from your version.
34  //
35  // A copy of the GNU General Public License is provided in the file gpl.txt. You
36  // may also obtain a copy of the GNU General Public License on the World Wide
37  // Web at http://www.gnu.org/licenses/gpl.html.
38  //
39  //******************************************************************************
40  package edu.rit.util;
41  
42  /**
43   * Interface TimerTask specifies the interface for an object that performs timed
44   * actions under the control of a {@linkplain Timer}.
45   * <P>
46   * When a timer is created, it is associated with a timer task. When the timer
47   * becomes triggered -- that is, when the time comes to do the timed actions --
48   * the timer calls the timer task's <code>action()</code> method. The timer passes a
49   * reference to itself as an argument to the timer task's <code>action()</code>
50   * method.
51   * <P>
52   * The first thing the timer task must do in the <code>action()</code> method is
53   * check whether the timer is still triggered. If it is, the <code>action()</code>
54   * method can perform its processing. But if the timer is no longer triggered,
55   * the <code>action()</code> method must return without doing anything.
56   * <P>
57   * This is to deal with a race condition that can arise when multiple threads
58   * are involved. Suppose the timer thread triggers the timer, the timer calls
59   * the timer task's <code>action()</code> method, and the <code>action()</code> method
60   * synchronizes on the object that will perform the action. Suppose the
61   * <code>action()</code> method blocks because some other thread is already
62   * executing a synchronized method on this object. Suppose the other thread
63   * cancels the timer. Here is the race condition: the timer was canceled just as
64   * it was triggered but before it could do the timed actions. When the other
65   * thread returns, the <code>action()</code> method unblocks and proceeds to
66   * execute. The <code>action()</code> method must check whether the timer got
67   * canceled between the time when the <code>action()</code> method was called and
68   * the time when the <code>action()</code> method started executing. If the
69   * <code>action()</code> method doesn't do this check, it may erroneously perform
70   * the timeout actions despite the timer cancellation.
71   * <P>
72   * Classes {@linkplain Timer}, TimerTask, and {@linkplain TimerThread} provide
73   * capabilities similar to classes java.util.Timer and java.util.TimerTask.
74   * Unlike the latter, they also provide the ability to stop and restart a timer
75   * and the ability to deal with race conditions in multithreaded programs.
76   *
77   * @author Alan Kaminsky
78   * @version 27-Sep-2002
79   */
80  public interface TimerTask {
81  
82  // Exported operations.
83      /**
84       * Perform this timer task's timed actions. The {@linkplain Timer} that was
85       * triggered is passed in as an argument.
86       * <P>
87       * The <code>action()</code> method must check whether the timer is still
88       * triggered. If it is, the <code>action()</code> method can perform its
89       * processing. But if the timer is no longer triggered, the
90       * <code>action()</code> method must return without doing anything.
91       *
92       * @param theTimer Timer that was triggered.
93       */
94      public void action(Timer theTimer);
95  
96  }