Source Code Cross Referenced for DefaultPTimer.java in  » 6.0-JDK-Modules » j2me » com » sun » 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 » 6.0 JDK Modules » j2me » com.sun.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)DefaultPTimer.java	1.11 06/10/10
003:         *
004:         * Copyright  1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006:         * 
007:         * This program is free software; you can redistribute it and/or
008:         * modify it under the terms of the GNU General Public License version
009:         * 2 only, as published by the Free Software Foundation. 
010:         * 
011:         * This program is distributed in the hope that it will be useful, but
012:         * WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014:         * General Public License version 2 for more details (a copy is
015:         * included at /legal/license.txt). 
016:         * 
017:         * You should have received a copy of the GNU General Public License
018:         * version 2 along with this work; if not, write to the Free Software
019:         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020:         * 02110-1301 USA 
021:         * 
022:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023:         * Clara, CA 95054 or visit www.sun.com if you need additional
024:         * information or have any questions. 
025:         *
026:         */
027:
028:        package com.sun.util;
029:
030:        import java.util.Timer;
031:        import java.util.TimerTask;
032:        import java.util.Date;
033:        import java.util.Map;
034:        import java.util.HashMap;
035:        import java.util.List;
036:        import java.util.LinkedList;
037:        import java.util.Iterator;
038:
039:        /** An implementaion of the PTimer class which uses the java.util.Timer class
040:         to implement scheduling. PTimer is now deprecated and so it is preferrable to
041:         use java.util.Timer instead. We also manage a saving of about 900 bytes in the
042:         process!
043:         */
044:
045:        final class DefaultPTimer extends PTimer {
046:            public void schedule(PTimerSpec spec) {
047:                // Create a TimerTask to notify the listeners of the
048:                // spec when it is run by the timer
049:
050:                TimerTask timerTask = new SpecTimerTask(spec, this );
051:                synchronized (this ) {
052:                    // Store a map of specs to tasks so that we can deschedule
053:                    // the tasks.
054:
055:                    Object tasks = specToTasksMap.get(spec);
056:                    if (tasks == null) {
057:                        // Haven't scheduled a task before so just store it directly
058:                        // in the Map. We don't create a List yet because it is unlikely
059:                        // that it will ever be required.
060:
061:                        specToTasksMap.put(spec, timerTask);
062:                    } else if (tasks instanceof  TimerTask) {
063:                        // One TimerTask already exists for this spec so we need to create a list
064:                        // to store the existing TimerTask and the new one.
065:
066:                        List taskList = new LinkedList();
067:                        taskList.add(tasks);
068:                        taskList.add(timerTask);
069:                        specToTasksMap.put(spec, taskList);
070:                    } else {
071:                        // Already scheduled more than one task for this PTimerSpec
072:                        // so add the new task to the list.
073:
074:                        synchronized (tasks) {
075:                            ((List) tasks).add(timerTask);
076:                        }
077:                    }
078:                }
079:                // Schedule the TimerTask on the Timer.
080:
081:                long time = spec.getTime();
082:                if (spec.isAbsolute())
083:                    timer.schedule(timerTask, new Date(time));
084:                else {
085:                    if (spec.isRepeat()) {
086:                        if (spec.isRegular())
087:                            timer.scheduleAtFixedRate(timerTask, time, time);
088:                        else
089:                            timer.schedule(timerTask, time, time);
090:                    } else
091:                        timer.schedule(timerTask, time);
092:                }
093:            }
094:
095:            public PTimerSpec scheduleTimerSpec(PTimerSpec t) {
096:                // It is more efficient to call it this way than from the deprecated method as we
097:                // would have to catch an exception that is never thrown.
098:
099:                schedule(t);
100:                return t;
101:            }
102:
103:            public void deschedule(PTimerSpec t) {
104:                Object tasks = null;
105:                synchronized (this ) {
106:                    tasks = specToTasksMap.get(t);
107:                    // No tasks to cancel
108:
109:                    if (tasks == null)
110:                        return;
111:                    specToTasksMap.remove(t);
112:                }
113:                // A List of tasks to cancel
114:
115:                if (tasks instanceof  List) {
116:                    Iterator i = ((List) tasks).iterator();
117:                    synchronized (tasks) {
118:                        while (i.hasNext())
119:                            ((TimerTask) i.next()).cancel();
120:                    }
121:                } // Just one task to cancel
122:                else
123:                    ((TimerTask) tasks).cancel();
124:            }
125:
126:            public long getMinRepeatInterval() {
127:                return (long) -1;
128:            }
129:
130:            public long getGranularity() {
131:                return (long) -1;
132:            }
133:
134:            /** The Timer used to implement the scheduling for this PTimer. */
135:
136:            private Timer timer = new Timer();
137:            /** A Map which maps PTimerSpecs to the TimerTasks which have been scheduled on the
138:             Timer. The key is a PTimerSpec and the value is either a List of TimerTasks
139:             running for that spec or just a single TimerTask object. This prevents creating
140:             a List when the majority of the time a PTimerSpec is only going to be scheduled
141:             once with a PTimer. */
142:
143:            private Map specToTasksMap = new HashMap();
144:        }
145:
146:        /** TimerTask which will notify the listeners of the corresponding PTimerSpec
147:         when it is run by the Timer. */
148:
149:        class SpecTimerTask extends TimerTask {
150:            /** Creates a new SpecTimerTask which will notify the listeners of the
151:             PTimerSpec. The PTimer supplied will be the source of events fired
152:             to the listeners. */
153:
154:            public SpecTimerTask(PTimerSpec spec, PTimer timer) {
155:                this .spec = spec;
156:                this .timer = timer;
157:            }
158:
159:            public void run() {
160:                spec.notifyListeners(timer);
161:            }
162:
163:            /** The PTimerSpec to notify listeners for. */
164:
165:            private PTimerSpec spec;
166:            /** The PTimer that created this task and is the source for PTimerWentOffEvents. */
167:
168:            private PTimer timer;
169:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.