Source Code Cross Referenced for TimeScheduler.java in  » Net » JGroups-2.4.1-sp3 » org » jgroups » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » JGroups 2.4.1 sp3 » org.jgroups.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.jgroups.util;
002:
003:        import org.apache.commons.logging.Log;
004:        import org.apache.commons.logging.LogFactory;
005:
006:        import java.util.Timer;
007:        import java.util.TimerTask;
008:        import java.util.Date;
009:
010:        /**
011:         * Fixed-delay & fixed-rate single thread scheduler
012:         * <p/>
013:         * The scheduler supports varying scheduling intervals by asking the task
014:         * every time for its next preferred scheduling interval. Scheduling can
015:         * either be <i>fixed-delay</i> or <i>fixed-rate</i>. The notions are
016:         * borrowed from <tt>java.util.Timer</tt> and retain the same meaning.
017:         * I.e. in fixed-delay scheduling, the task's new schedule is calculated
018:         * as:<br>
019:         * new_schedule = time_task_starts + scheduling_interval
020:         * <p/>
021:         * In fixed-rate scheduling, the next schedule is calculated as:<br>
022:         * new_schedule = time_task_was_supposed_to_start + scheduling_interval
023:         * <p/>
024:         * The scheduler internally holds a queue of tasks sorted in ascending order
025:         * according to their next execution time. A task is removed from the queue
026:         * if it is cancelled, i.e. if <tt>TimeScheduler.Task.isCancelled()</tt>
027:         * returns true.
028:         * <p/>
029:         * The scheduler internally uses a <tt>java.util.SortedSet</tt> to keep tasks
030:         * sorted. <tt>java.util.Timer</tt> uses an array arranged as a binary heap
031:         * that doesn't shrink. It is likely that the latter arrangement is faster.
032:         * <p/>
033:         * Initially, the scheduler is in <tt>SUSPEND</tt>ed mode, <tt>start()</tt>
034:         * need not be called: if a task is added, the scheduler gets started
035:         * automatically. Calling <tt>start()</tt> starts the scheduler if it's
036:         * suspended or stopped else has no effect. Once <tt>stop()</tt> is called,
037:         * added tasks will not restart it: <tt>start()</tt> has to be called to
038:         * restart the scheduler.
039:         * @author Bela Ban
040:         * @version $Id: TimeScheduler.java,v 1.14.2.2 2007/04/27 09:11:18 belaban Exp $
041:         */
042:        public class TimeScheduler extends Timer {
043:            /**
044:             * The interface that submitted tasks must implement
045:             */
046:            public interface Task {
047:                /**
048:                 * @return true if task is cancelled and shouldn't be scheduled
049:                 *         again
050:                 */
051:                boolean cancelled();
052:
053:                /**
054:                 * @return the next schedule interval
055:                 */
056:                long nextInterval();
057:
058:                /**
059:                 * Execute the task
060:                 */
061:                void run();
062:            }
063:
064:            public interface CancellableTask extends Task {
065:                /**
066:                 * Cancels the task. After calling this, {@link #cancelled()} return true. If the task was already cancelled,
067:                 * this is a no-op
068:                 */
069:                void cancel();
070:            }
071:
072:            private int size = 0; // maintains the number of tasks currently scheduled to execute
073:
074:            protected static final Log log = LogFactory
075:                    .getLog(TimeScheduler.class);
076:
077:            public TimeScheduler() {
078:                super (true);
079:            }
080:
081:            public TimeScheduler(boolean isDaemon) {
082:                super (isDaemon);
083:            }
084:
085:            public String dumpTaskQueue() {
086:                return toString();
087:            }
088:
089:            /**
090:             * Add a task for execution at adjustable intervals
091:             * @param task     the task to execute
092:             * @param relative scheduling scheme:
093:             *                 <p/>
094:             *                 <tt>true</tt>:<br>
095:             *                 Task is rescheduled relative to the last time it <i>actually</i>
096:             *                 started execution
097:             *                 <p/>
098:             *                 <tt>false</tt>:<br>
099:             *                 Task is scheduled relative to its <i>last</i> execution schedule. This
100:             *                 has the effect that the time between two consecutive executions of
101:             *                 the task remains the same.<p/>
102:             * April 07: the relative argument is ignored, will always be true
103:             */
104:            public void add(Task task, boolean relative) {
105:                TaskWrapper wrapper = new TaskWrapper(task);
106:                schedule(wrapper, task.nextInterval());
107:            }
108:
109:            /**
110:             * Add a task for execution at adjustable intervals
111:             * @param t the task to execute
112:             */
113:            public void add(Task t) {
114:                add(t, true);
115:            }
116:
117:            public void schedule(TimerTask task, long delay) {
118:                super .schedule(task, delay);
119:                size++;
120:            }
121:
122:            public void schedule(TimerTask task, long delay, long period) {
123:                super .schedule(task, delay, period);
124:                size++;
125:            }
126:
127:            public void schedule(TimerTask task, Date firstTime, long period) {
128:                super .schedule(task, firstTime, period);
129:                size++;
130:            }
131:
132:            public void schedule(TimerTask task, Date time) {
133:                super .schedule(task, time);
134:                size++;
135:            }
136:
137:            public void scheduleAtFixedRate(TimerTask task, long delay,
138:                    long period) {
139:                super .scheduleAtFixedRate(task, delay, period);
140:                size++;
141:            }
142:
143:            public void scheduleAtFixedRate(TimerTask task, Date firstTime,
144:                    long period) {
145:                super .scheduleAtFixedRate(task, firstTime, period);
146:                size++;
147:            }
148:
149:            public void cancel() {
150:                super .cancel();
151:                size = 0;
152:            }
153:
154:            /**
155:             * Returns the number of tasks currently scheduled. Note that this is an approximation.
156:             * @return The number of tasks currently in the queue.
157:             */
158:            public int size() {
159:                return size;
160:            }
161:
162:            /**
163:             * Start the scheduler, if it's suspended or stopped
164:             */
165:            public void start() {
166:                ; // no-op
167:            }
168:
169:            /**
170:             * Stop the scheduler if it's running. Switch to stopped, if it's
171:             * suspended. Clear the task queue.
172:             *
173:             * @throws InterruptedException if interrupted while waiting for thread
174:             *                              to return
175:             */
176:            public void stop() throws InterruptedException {
177:            }
178:
179:            private class TaskWrapper extends TimerTask {
180:                private final Task delegate; // points to the user-submitted task
181:
182:                public TaskWrapper(Task delegate) {
183:                    this .delegate = delegate;
184:                }
185:
186:                public void run() {
187:                    if (delegate.cancelled()) {
188:                        cancel();
189:                        return;
190:                    }
191:                    try {
192:                        delegate.run();
193:                    } catch (Throwable t) {
194:                        if (log.isWarnEnabled()) {
195:                            log.warn("exception executing task " + delegate, t);
196:                        }
197:                    }
198:                    size = Math.max(size - 1, 0);
199:                    if (!delegate.cancelled()) {
200:                        long next_interval = delegate.nextInterval();
201:                        TimerTask new_task = new TaskWrapper(delegate);
202:                        schedule(new_task, next_interval);
203:                    }
204:                }
205:
206:                public boolean cancel() {
207:                    size = Math.max(0, size - 1);
208:                    return super.cancel();
209:                }
210:            }
211:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.