View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.util;
21  
22  import java.util.Timer;
23  import java.util.TimerTask;
24  
25  /***
26   * Provides a single thread scheduler for maintenance tasks. 
27   *
28   * <p>All tasks are executed sequentially. Therefore tasks should complete 
29   * quickly. If a timer task takes excessive time to complete, it might block
30   * the execution of subsequent tasks.</p>
31   *
32   * @see java.util.Timer
33   */
34  public class Scheduler {
35      
36      //--- Constant(s) ---
37  
38      //--- Data field(s) ---
39  
40      private static Timer timer = new Timer(true);
41  
42      //--- Constructor(s) ---
43  
44      private Scheduler()
45      {
46      }
47  
48      //--- Method(s) ---
49  
50      /***
51       * Schedules <code>task</code> for execution in <code>delay</code> milli
52       * seconds.
53       */
54      public static void run(long delay, Runnable task) 
55      {
56  		timer.schedule(new SimpleTimerTask(task).getTimerTask(), delay);
57      }
58  
59  	/***
60       * Use {@link #run(long, XNapTask)} instead.
61       * 
62       * @deprecated
63       */
64  	public static void run(long delay, TimerTask task)
65  	{
66  		timer.schedule(task, delay);
67  	}
68  
69  	/***
70       * Schedules <code>task</code> for execution in <code>delay</code> milli
71       * seconds.
72       */
73  	public static void run(long delay, XNapTask task)
74  	{
75  		timer.schedule(task.getTimerTask(), delay);
76  	}
77  
78      /***
79       * Schedules <code>task</code> for execution in <code>delay</code> milli
80       * seconds and from that point for repeated executions 
81       * in <code>interval</code> milli seconds.
82       */
83      public static void run(long delay, long interval, Runnable task) 
84      {
85  		timer.schedule(new SimpleTimerTask(task).getTimerTask(), delay, interval);
86      }
87  
88      /***
89       * Use {@link #run(long, long, XNapTask)} instead.
90       * 
91       * @deprecated 
92       */
93      public static void run(long delay, long interval, TimerTask task) 
94      {
95  		timer.schedule(task, delay, interval);
96      }
97  
98      /***
99       * Schedules <code>task</code> for execution in <code>delay</code> milli
100      * seconds and from that point for repeated executions 
101      * in <code>interval</code> milli seconds.
102      */
103     public static void run(long delay, long interval, XNapTask task) 
104     {
105 		timer.schedule(task.getTimerTask(), delay, interval);
106     }
107 
108     /***
109      * Provides a simple timer task that executes a runnable.
110      */
111     public static class SimpleTimerTask extends XNapTask {
112 
113 		Runnable runnable;
114 	
115 		public SimpleTimerTask(Runnable runnable) 
116 		{
117 			this.runnable = runnable;
118 		}
119 
120 		public void run() 
121 		{
122 			runnable.run();
123 		}
124 
125     }
126 
127 }
128 
129 
130 
131 
132