KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > util > TaskScheduler


1 /*_############################################################################
2   _##
3   _## SNMP4J - TaskScheduler.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21
22
23 package org.snmp4j.util;
24
25 import java.util.LinkedList JavaDoc;
26 import org.snmp4j.log.LogFactory;
27 import org.snmp4j.log.LogAdapter;
28
29 /**
30  * The <code>TaskScheduler</code> uses a <code>ThreadPool</code> to recurrent
31  * execute <code>SchedulerTask</code>s.
32  *
33  * @author Frank Fock
34  * @version 1.6
35  * @since 1.6
36  */

37 public class TaskScheduler implements Runnable JavaDoc {
38
39   private LogAdapter logger = LogFactory.getLogger(TaskScheduler.class);
40
41   private static final long DEFAULT_SCHEDULER_TIMEOUT = 5;
42
43   private LinkedList JavaDoc tasks = new LinkedList JavaDoc();
44   private ThreadPool threadPool;
45   private boolean stop;
46   protected long schedulerTimeout = DEFAULT_SCHEDULER_TIMEOUT;
47
48   /**
49    * Creates a <code>TaskScheduler</code> that uses the supplied
50    * <code>ThreadPool</code> to execute tasks.
51    *
52    * @param threadPool
53    * a <code>ThreadPool</code>.
54    */

55   public TaskScheduler(ThreadPool threadPool) {
56     this.threadPool = threadPool;
57   }
58
59   /**
60    * Adds a task to the scheduler.
61    * @param task
62    * a <code>SchedulerTask</code>.
63    */

64   public synchronized void addTask(SchedulerTask task) {
65     tasks.addLast(task);
66     notify();
67   }
68
69   /**
70    * Removes a task from the scheduler.
71    * @param task
72    * the <code>SchedulerTask</code> to be removed from the scheduler
73    * @return
74    * <code>true</code> if the task could be removed.
75    */

76   public synchronized boolean removeTask(SchedulerTask task) {
77     return tasks.remove(task);
78   }
79
80   /**
81    * Removes all tasks.
82    */

83   public synchronized void clear() {
84     tasks.clear();
85   }
86
87   /**
88    * Runs the scheduler. While in this method tasks are scheduled on the
89    * internal thread pool. The scheduler tries to schedule task fairly.
90    */

91   public void run() {
92     while (!stop) {
93       boolean readyToRun = false;
94       synchronized (this) {
95         for (int i=0; i<tasks.size(); i++) {
96           SchedulerTask task = (SchedulerTask) tasks.get(i);
97           if (task.isDone()) {
98             if (logger.isDebugEnabled()) {
99               logger.debug("Task '" + task + "' is done");
100             }
101             tasks.removeFirst();
102             continue;
103           }
104           else if (task.isReadyToRun()) {
105             readyToRun = true;
106             while (!threadPool.tryToExecute(task)) {
107               try {
108                 synchronized (threadPool) {
109                   threadPool.wait(schedulerTimeout);
110                 }
111               }
112               catch (InterruptedException JavaDoc ex) {
113                 logger.warn("Scheduler interrupted, aborting...");
114                 stop = true;
115                 break;
116               }
117             }
118             tasks.addLast(tasks.removeFirst());
119             i--;
120           }
121         }
122       }
123       if (!readyToRun) {
124         try {
125           if (threadPool.isIdle()) {
126             synchronized (this) {
127               wait(schedulerTimeout);
128             }
129           }
130           else {
131             synchronized (threadPool) {
132               threadPool.wait(schedulerTimeout);
133             }
134           }
135         }
136         catch (InterruptedException JavaDoc ex1) {
137           logger.warn("Scheduler interrupted, aborting...");
138           stop = true;
139         }
140       }
141     }
142     logger.info("Scheduler stopped.");
143   }
144
145   /**
146    * Stops the schedulers run method.
147    * @param stop
148    * <code>true</code> to stop the scheduler.
149    */

150   public void setStop(boolean stop) {
151     this.stop = stop;
152   }
153
154   /**
155    * Checks if the scheduler is (to be) stopped.
156    * @return
157    * <code>true</code> if the scheduler has been stopped or is being stopped.
158    */

159   public boolean isStop() {
160     return stop;
161   }
162 }
163
Popular Tags