2634 |
12 Sep 06 |
nicklas |
1 |
/** |
2634 |
12 Sep 06 |
nicklas |
$Id$ |
2634 |
12 Sep 06 |
nicklas |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2006 Nicklas Nordborg |
2634 |
12 Sep 06 |
nicklas |
5 |
|
2634 |
12 Sep 06 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
2634 |
12 Sep 06 |
nicklas |
Available at http://base.thep.lu.se/ |
2634 |
12 Sep 06 |
nicklas |
8 |
|
2634 |
12 Sep 06 |
nicklas |
BASE is free software; you can redistribute it and/or |
2634 |
12 Sep 06 |
nicklas |
modify it under the terms of the GNU General Public License |
4479 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
2634 |
12 Sep 06 |
nicklas |
of the License, or (at your option) any later version. |
2634 |
12 Sep 06 |
nicklas |
13 |
|
2634 |
12 Sep 06 |
nicklas |
BASE is distributed in the hope that it will be useful, |
2634 |
12 Sep 06 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
2634 |
12 Sep 06 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2634 |
12 Sep 06 |
nicklas |
GNU General Public License for more details. |
2634 |
12 Sep 06 |
nicklas |
18 |
|
2634 |
12 Sep 06 |
nicklas |
You should have received a copy of the GNU General Public License |
4515 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
2634 |
12 Sep 06 |
nicklas |
21 |
*/ |
2634 |
12 Sep 06 |
nicklas |
22 |
package net.sf.basedb.util.timer; |
2634 |
12 Sep 06 |
nicklas |
23 |
|
2634 |
12 Sep 06 |
nicklas |
24 |
import java.util.Date; |
2634 |
12 Sep 06 |
nicklas |
25 |
import java.util.Timer; |
2634 |
12 Sep 06 |
nicklas |
26 |
import java.util.TimerTask; |
2634 |
12 Sep 06 |
nicklas |
27 |
|
2634 |
12 Sep 06 |
nicklas |
28 |
/** |
2634 |
12 Sep 06 |
nicklas |
This class is a cousin to the standard {@link Timer} class |
2634 |
12 Sep 06 |
nicklas |
but only allows scheduling of tasks. This allows an application to |
2634 |
12 Sep 06 |
nicklas |
create a core timer and let other code add {@link TimerTask}:s to |
2634 |
12 Sep 06 |
nicklas |
the timer. |
2634 |
12 Sep 06 |
nicklas |
<p> |
2634 |
12 Sep 06 |
nicklas |
Another feature of this class is that it can force the execution of |
2634 |
12 Sep 06 |
nicklas |
tasks in their own threads. This avoids that long-runing tasks hog the |
2634 |
12 Sep 06 |
nicklas |
timer thread and therby prevent other tasks from running. |
2634 |
12 Sep 06 |
nicklas |
<p> |
2634 |
12 Sep 06 |
nicklas |
If new threads are created for the tasks, it is also possible to |
2634 |
12 Sep 06 |
nicklas |
specify on a task-by-task basis if the task may run in multiple |
2634 |
12 Sep 06 |
nicklas |
threads at the same time. |
2634 |
12 Sep 06 |
nicklas |
<p> |
2634 |
12 Sep 06 |
nicklas |
A side-effect by using a scheduler is that all methods may return |
2634 |
12 Sep 06 |
nicklas |
a different <code>TimerTask</code> object than what was scheduled. |
2634 |
12 Sep 06 |
nicklas |
If the calling application want to cancel a task it must do so on |
2634 |
12 Sep 06 |
nicklas |
the object that is returned, since that object is the one really lives |
2634 |
12 Sep 06 |
nicklas |
in the timer. However, calling {@link TimerTask#cancel()} passes through |
2634 |
12 Sep 06 |
nicklas |
to the original task as well. |
2634 |
12 Sep 06 |
nicklas |
48 |
|
2634 |
12 Sep 06 |
nicklas |
@author nicklas |
2634 |
12 Sep 06 |
nicklas |
@version 2.0 |
2634 |
12 Sep 06 |
nicklas |
@base.modified $Date$ |
2634 |
12 Sep 06 |
nicklas |
@see Timer |
2634 |
12 Sep 06 |
nicklas |
@see TimerTask |
2634 |
12 Sep 06 |
nicklas |
@see ThreadTimerTask |
2634 |
12 Sep 06 |
nicklas |
55 |
*/ |
2634 |
12 Sep 06 |
nicklas |
56 |
public class Scheduler |
2634 |
12 Sep 06 |
nicklas |
57 |
{ |
2634 |
12 Sep 06 |
nicklas |
58 |
private final Timer timer; |
2634 |
12 Sep 06 |
nicklas |
59 |
private final boolean newThreads; |
2634 |
12 Sep 06 |
nicklas |
60 |
/** |
2634 |
12 Sep 06 |
nicklas |
Create a new scheduler object. |
2634 |
12 Sep 06 |
nicklas |
62 |
|
2634 |
12 Sep 06 |
nicklas |
@param timer The underlying timer object which handles the execution of the tasks |
2634 |
12 Sep 06 |
nicklas |
@param newThreads If TRUE, each task is started in it's own thread, if FALSE |
2634 |
12 Sep 06 |
nicklas |
the tasks are executed in the timers thread |
2634 |
12 Sep 06 |
nicklas |
66 |
*/ |
2634 |
12 Sep 06 |
nicklas |
67 |
public Scheduler(Timer timer, boolean newThreads) |
2634 |
12 Sep 06 |
nicklas |
68 |
{ |
2634 |
12 Sep 06 |
nicklas |
69 |
this.timer = timer; |
2634 |
12 Sep 06 |
nicklas |
70 |
this.newThreads = newThreads; |
2634 |
12 Sep 06 |
nicklas |
71 |
} |
2634 |
12 Sep 06 |
nicklas |
72 |
|
2634 |
12 Sep 06 |
nicklas |
73 |
private TimerTask getTimerTask(TimerTask task, boolean allowMultiple) |
2634 |
12 Sep 06 |
nicklas |
74 |
{ |
2634 |
12 Sep 06 |
nicklas |
75 |
return newThreads ? new ThreadTimerTask(task, allowMultiple) : task; |
2634 |
12 Sep 06 |
nicklas |
76 |
} |
2634 |
12 Sep 06 |
nicklas |
77 |
|
2634 |
12 Sep 06 |
nicklas |
78 |
/** |
2634 |
12 Sep 06 |
nicklas |
@see Timer#schedule(TimerTask, Date) |
2634 |
12 Sep 06 |
nicklas |
80 |
*/ |
2634 |
12 Sep 06 |
nicklas |
81 |
public TimerTask schedule(TimerTask task, Date time, boolean allowMultiple) |
2634 |
12 Sep 06 |
nicklas |
82 |
{ |
2634 |
12 Sep 06 |
nicklas |
83 |
task = getTimerTask(task, allowMultiple); |
2634 |
12 Sep 06 |
nicklas |
84 |
timer.schedule(task, time); |
2634 |
12 Sep 06 |
nicklas |
85 |
return task; |
2634 |
12 Sep 06 |
nicklas |
86 |
} |
2634 |
12 Sep 06 |
nicklas |
87 |
|
2634 |
12 Sep 06 |
nicklas |
88 |
/** |
2634 |
12 Sep 06 |
nicklas |
@see Timer#schedule(TimerTask, Date, long) |
2634 |
12 Sep 06 |
nicklas |
90 |
*/ |
2634 |
12 Sep 06 |
nicklas |
91 |
public TimerTask schedule(TimerTask task, Date firstTime, long period, boolean allowMultiple) |
2634 |
12 Sep 06 |
nicklas |
92 |
{ |
2634 |
12 Sep 06 |
nicklas |
93 |
task = getTimerTask(task, allowMultiple); |
2634 |
12 Sep 06 |
nicklas |
94 |
timer.schedule(task, firstTime, period); |
2634 |
12 Sep 06 |
nicklas |
95 |
return task; |
2634 |
12 Sep 06 |
nicklas |
96 |
} |
2634 |
12 Sep 06 |
nicklas |
97 |
|
2634 |
12 Sep 06 |
nicklas |
98 |
/** |
2634 |
12 Sep 06 |
nicklas |
@see Timer#schedule(TimerTask, long) |
2634 |
12 Sep 06 |
nicklas |
100 |
*/ |
2634 |
12 Sep 06 |
nicklas |
101 |
public TimerTask schedule(TimerTask task, long delay, boolean allowMultiple) |
2634 |
12 Sep 06 |
nicklas |
102 |
{ |
2634 |
12 Sep 06 |
nicklas |
103 |
task = getTimerTask(task, allowMultiple); |
2634 |
12 Sep 06 |
nicklas |
104 |
timer.schedule(task, delay); |
2634 |
12 Sep 06 |
nicklas |
105 |
return task; |
2634 |
12 Sep 06 |
nicklas |
106 |
} |
2634 |
12 Sep 06 |
nicklas |
107 |
|
2634 |
12 Sep 06 |
nicklas |
108 |
/** |
2634 |
12 Sep 06 |
nicklas |
@see Timer#schedule(TimerTask, long, long) |
2634 |
12 Sep 06 |
nicklas |
110 |
*/ |
2634 |
12 Sep 06 |
nicklas |
111 |
public TimerTask schedule(TimerTask task, long delay, long period, boolean allowMultiple) |
2634 |
12 Sep 06 |
nicklas |
112 |
{ |
2634 |
12 Sep 06 |
nicklas |
113 |
task = getTimerTask(task, allowMultiple); |
2634 |
12 Sep 06 |
nicklas |
114 |
timer.schedule(task, delay, period); |
2634 |
12 Sep 06 |
nicklas |
115 |
return task; |
2634 |
12 Sep 06 |
nicklas |
116 |
} |
2634 |
12 Sep 06 |
nicklas |
117 |
|
2634 |
12 Sep 06 |
nicklas |
118 |
/** |
2634 |
12 Sep 06 |
nicklas |
@see Timer#scheduleAtFixedRate(TimerTask, Date, long) |
2634 |
12 Sep 06 |
nicklas |
120 |
*/ |
2634 |
12 Sep 06 |
nicklas |
121 |
public TimerTask scheduleAtFixedRate(TimerTask task, Date firstTime, long period, boolean allowMultiple) |
2634 |
12 Sep 06 |
nicklas |
122 |
{ |
2634 |
12 Sep 06 |
nicklas |
123 |
task = getTimerTask(task, allowMultiple); |
2634 |
12 Sep 06 |
nicklas |
124 |
timer.scheduleAtFixedRate(task, firstTime, period); |
2634 |
12 Sep 06 |
nicklas |
125 |
return task; |
2634 |
12 Sep 06 |
nicklas |
126 |
} |
2634 |
12 Sep 06 |
nicklas |
127 |
|
2634 |
12 Sep 06 |
nicklas |
128 |
/** |
2634 |
12 Sep 06 |
nicklas |
@see Timer#scheduleAtFixedRate(TimerTask, long, long) |
2634 |
12 Sep 06 |
nicklas |
130 |
*/ |
2634 |
12 Sep 06 |
nicklas |
131 |
public TimerTask scheduleAtFixedRate(TimerTask task, long delay, long period, boolean allowMultiple) |
2634 |
12 Sep 06 |
nicklas |
132 |
{ |
2634 |
12 Sep 06 |
nicklas |
133 |
task = getTimerTask(task, allowMultiple); |
2634 |
12 Sep 06 |
nicklas |
134 |
timer.scheduleAtFixedRate(task, delay, period); |
2634 |
12 Sep 06 |
nicklas |
135 |
return task; |
2634 |
12 Sep 06 |
nicklas |
136 |
} |
2634 |
12 Sep 06 |
nicklas |
137 |
|
2634 |
12 Sep 06 |
nicklas |
138 |
} |