src/core/net/sf/basedb/util/timer/Scheduler.java

Code
Comments
Other
Rev Date Author Line
2634 12 Sep 06 nicklas 1 /**
2634 12 Sep 06 nicklas 2   $Id$
2634 12 Sep 06 nicklas 3
3675 16 Aug 07 jari 4   Copyright (C) 2006 Nicklas Nordborg
2634 12 Sep 06 nicklas 5
2634 12 Sep 06 nicklas 6   This file is part of BASE - BioArray Software Environment.
2634 12 Sep 06 nicklas 7   Available at http://base.thep.lu.se/
2634 12 Sep 06 nicklas 8
2634 12 Sep 06 nicklas 9   BASE is free software; you can redistribute it and/or
2634 12 Sep 06 nicklas 10   modify it under the terms of the GNU General Public License
4479 05 Sep 08 jari 11   as published by the Free Software Foundation; either version 3
2634 12 Sep 06 nicklas 12   of the License, or (at your option) any later version.
2634 12 Sep 06 nicklas 13
2634 12 Sep 06 nicklas 14   BASE is distributed in the hope that it will be useful,
2634 12 Sep 06 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
2634 12 Sep 06 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2634 12 Sep 06 nicklas 17   GNU General Public License for more details.
2634 12 Sep 06 nicklas 18
2634 12 Sep 06 nicklas 19   You should have received a copy of the GNU General Public License
4515 11 Sep 08 jari 20   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 29   This class is a cousin to the standard {@link Timer} class
2634 12 Sep 06 nicklas 30   but only allows scheduling of tasks. This allows an application to
2634 12 Sep 06 nicklas 31   create a core timer and let other code add {@link TimerTask}:s to
2634 12 Sep 06 nicklas 32   the timer.
2634 12 Sep 06 nicklas 33   <p>
2634 12 Sep 06 nicklas 34   Another feature of this class is that it can force the execution of
2634 12 Sep 06 nicklas 35   tasks in their own threads. This avoids that long-runing tasks hog the
2634 12 Sep 06 nicklas 36   timer thread and therby prevent other tasks from running.
2634 12 Sep 06 nicklas 37   <p>
2634 12 Sep 06 nicklas 38   If new threads are created for the tasks, it is also possible to
2634 12 Sep 06 nicklas 39   specify on a task-by-task basis if the task may run in multiple 
2634 12 Sep 06 nicklas 40   threads at the same time.
2634 12 Sep 06 nicklas 41   <p>
2634 12 Sep 06 nicklas 42   A side-effect by using a scheduler is that all methods may return
2634 12 Sep 06 nicklas 43   a different <code>TimerTask</code> object than what was scheduled. 
2634 12 Sep 06 nicklas 44   If the calling application want to cancel a task it must do so on
2634 12 Sep 06 nicklas 45   the object that is returned, since that object is the one really lives
2634 12 Sep 06 nicklas 46   in the timer. However, calling {@link TimerTask#cancel()} passes through
2634 12 Sep 06 nicklas 47   to the original task as well.
2634 12 Sep 06 nicklas 48
2634 12 Sep 06 nicklas 49   @author nicklas
2634 12 Sep 06 nicklas 50   @version 2.0
2634 12 Sep 06 nicklas 51   @base.modified $Date$
2634 12 Sep 06 nicklas 52   @see Timer
2634 12 Sep 06 nicklas 53   @see TimerTask
2634 12 Sep 06 nicklas 54   @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 61     Create a new scheduler object. 
2634 12 Sep 06 nicklas 62     
2634 12 Sep 06 nicklas 63     @param timer The underlying timer object which handles the execution of the tasks
2634 12 Sep 06 nicklas 64     @param newThreads If TRUE, each task is started in it's own thread, if FALSE
2634 12 Sep 06 nicklas 65       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 79     @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 89     @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 99     @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 109     @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 119     @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 129     @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 }