src/core/net/sf/basedb/util/timer/ThreadTimerTask.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.TimerTask;
2634 12 Sep 06 nicklas 25
2634 12 Sep 06 nicklas 26 /**
2634 12 Sep 06 nicklas 27   Class for forcing the execution of a <code>TimerTask</code> in
2634 12 Sep 06 nicklas 28   a new thread. Instead of adding the original <code>TimerTask</code>
2634 12 Sep 06 nicklas 29   to a <code>Timer</code> an instance of this class is added, which
2634 12 Sep 06 nicklas 30   creates a new <code>Thread</code> where the original <code>TimerTask</code>:s
2634 12 Sep 06 nicklas 31   run method is executed.
2634 12 Sep 06 nicklas 32   <p>
2634 12 Sep 06 nicklas 33   Note that for repeating tasks the use of this class may lead to that
2634 12 Sep 06 nicklas 34   the same task is executed again before the previous execution has 
2634 12 Sep 06 nicklas 35   finished. This may lead to complications if the task is not thread-safe.
2634 12 Sep 06 nicklas 36
2634 12 Sep 06 nicklas 37   @author nicklas
2634 12 Sep 06 nicklas 38   @version 2.0
2634 12 Sep 06 nicklas 39   @base.modified $Date$
2634 12 Sep 06 nicklas 40 */
2634 12 Sep 06 nicklas 41 public class ThreadTimerTask
2634 12 Sep 06 nicklas 42   extends TimerTask
2634 12 Sep 06 nicklas 43 {
2634 12 Sep 06 nicklas 44
7786 02 Mar 20 nicklas 45   private static final org.slf4j.Logger log = 
7786 02 Mar 20 nicklas 46     org.slf4j.LoggerFactory.getLogger(ThreadTimerTask.class);
7786 02 Mar 20 nicklas 47
2634 12 Sep 06 nicklas 48   private final TimerTask task;
2634 12 Sep 06 nicklas 49   private final boolean allowMultiple;
2634 12 Sep 06 nicklas 50   
2634 12 Sep 06 nicklas 51   private boolean isExecuting;
2634 12 Sep 06 nicklas 52   
2634 12 Sep 06 nicklas 53   /**
2634 12 Sep 06 nicklas 54     Create a new threaded timer task. The task is executed in a new thread.
2634 12 Sep 06 nicklas 55     @param task The task to execute when the timer fires
2634 12 Sep 06 nicklas 56     @param allowMultiple If TRUE, it is allowed to start the same task
2634 12 Sep 06 nicklas 57       again before the previous execution has returned, if FALSE 
2634 12 Sep 06 nicklas 58       the timer event is ignored
2634 12 Sep 06 nicklas 59   */
2634 12 Sep 06 nicklas 60   public ThreadTimerTask(TimerTask task, boolean allowMultiple)
2634 12 Sep 06 nicklas 61   {
2634 12 Sep 06 nicklas 62     this.task = task;
2634 12 Sep 06 nicklas 63     this.allowMultiple = allowMultiple;
2634 12 Sep 06 nicklas 64     this.isExecuting = false;
2634 12 Sep 06 nicklas 65   }
2634 12 Sep 06 nicklas 66
2634 12 Sep 06 nicklas 67   /*
2634 12 Sep 06 nicklas 68     From the TimerTask class
2634 12 Sep 06 nicklas 69     -------------------------------------------
2634 12 Sep 06 nicklas 70   */
6127 14 Sep 12 nicklas 71   @Override
2634 12 Sep 06 nicklas 72   public final synchronized void run()
2634 12 Sep 06 nicklas 73   {
7786 02 Mar 20 nicklas 74     try
2634 12 Sep 06 nicklas 75     {
7786 02 Mar 20 nicklas 76       Thread t = null;
7786 02 Mar 20 nicklas 77       if (allowMultiple) 
7786 02 Mar 20 nicklas 78       {
7786 02 Mar 20 nicklas 79         // We just create another thread
7786 02 Mar 20 nicklas 80         t = new Thread(task);
7786 02 Mar 20 nicklas 81       }
7786 02 Mar 20 nicklas 82       else if (!isExecuting)
7786 02 Mar 20 nicklas 83       {
7786 02 Mar 20 nicklas 84         // Create a new thread that also manages the isExecuting flag
7786 02 Mar 20 nicklas 85         isExecuting = true;
7786 02 Mar 20 nicklas 86         t = new Thread(
7786 02 Mar 20 nicklas 87           new Runnable()
2634 12 Sep 06 nicklas 88           {
7786 02 Mar 20 nicklas 89             @Override
7786 02 Mar 20 nicklas 90             public void run()
6684 14 Jan 15 nicklas 91             {
7786 02 Mar 20 nicklas 92               try
7786 02 Mar 20 nicklas 93               {
7786 02 Mar 20 nicklas 94                 task.run();
7786 02 Mar 20 nicklas 95               }
7786 02 Mar 20 nicklas 96               finally
7786 02 Mar 20 nicklas 97               {
7786 02 Mar 20 nicklas 98                 isExecuting = false;
7786 02 Mar 20 nicklas 99               }
6684 14 Jan 15 nicklas 100             }
2634 12 Sep 06 nicklas 101           }
7786 02 Mar 20 nicklas 102         );
7786 02 Mar 20 nicklas 103       }
7786 02 Mar 20 nicklas 104       else
7786 02 Mar 20 nicklas 105       {
7786 02 Mar 20 nicklas 106         // Do not allow multiple thread to execute the task
7786 02 Mar 20 nicklas 107       }
7786 02 Mar 20 nicklas 108       // Start the thread
7786 02 Mar 20 nicklas 109       if (t != null) t.start();
2634 12 Sep 06 nicklas 110     }
7786 02 Mar 20 nicklas 111     catch (Throwable t)
2634 12 Sep 06 nicklas 112     {
7786 02 Mar 20 nicklas 113       log.error("Failed to start task: " + task, t);
2634 12 Sep 06 nicklas 114     }
2634 12 Sep 06 nicklas 115   }
6127 14 Sep 12 nicklas 116   @Override
2634 12 Sep 06 nicklas 117   public boolean cancel()
2634 12 Sep 06 nicklas 118   {
2634 12 Sep 06 nicklas 119     task.cancel();
2634 12 Sep 06 nicklas 120     return super.cancel();
2634 12 Sep 06 nicklas 121   }
2634 12 Sep 06 nicklas 122   // -------------------------------------------
2634 12 Sep 06 nicklas 123
2634 12 Sep 06 nicklas 124 }