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


001:        /*
002:         * @(#)GC.java	1.15 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 sun.misc;
029:
030:        import java.security.AccessController;
031:        import java.security.PrivilegedAction;
032:        import java.util.SortedSet;
033:        import java.util.TreeSet;
034:        import sun.misc.ThreadRegistry;
035:
036:        /**
037:         * Support for garbage-collection latency requests.
038:         *
039:         * @version  1.8, 00/05/16
040:         * @author   Mark Reinhold
041:         * @since    JDK1.2
042:         */
043:
044:        public class GC {
045:
046:            private GC() {
047:            } /* To prevent instantiation */
048:
049:            /* Latency-target value indicating that there's no active target
050:             */
051:            private static final long NO_TARGET = Long.MAX_VALUE;
052:
053:            /* The current latency target, or NO_TARGET if there is no target
054:             */
055:            private static long latencyTarget = NO_TARGET;
056:
057:            /* The daemon thread that implements the latency-target mechanism,
058:             * or null if there is presently no daemon thread
059:             */
060:            private static Thread daemon = null;
061:
062:            /* The lock object for the latencyTarget and daemon fields.  The daemon
063:             * thread, if it exists, waits on this lock for notification that the
064:             * latency target has changed.
065:             */
066:            private static class LatencyLock extends Object {
067:            };
068:
069:            private static Object lock = new LatencyLock();
070:
071:            /**
072:             * Returns the maximum <em>object-inspection age</em>, which is the number
073:             * of real-time milliseconds that have elapsed since the
074:             * least-recently-inspected heap object was last inspected by the garbage
075:             * collector.
076:             *
077:             * <p> For simple stop-the-world collectors this value is just the time
078:             * since the most recent collection.  For generational collectors it is the
079:             * time since the oldest generation was most recently collected.  Other
080:             * collectors are free to return a pessimistic estimate of the elapsed
081:             * time, or simply the time since the last full collection was performed.
082:             *
083:             * <p> Note that in the presence of reference objects, a given object that
084:             * is no longer strongly reachable may have to be inspected multiple times
085:             * before it can be reclaimed.
086:             */
087:            public static native long maxObjectInspectionAge();
088:
089:            private static class Daemon extends Thread {
090:
091:                public void run() {
092:                    while (!ThreadRegistry.exitRequested()) {
093:                        long l;
094:                        synchronized (lock) {
095:
096:                            l = latencyTarget;
097:                            if (l == NO_TARGET) {
098:                                /* No latency target, so exit */
099:                                GC.daemon = null;
100:                                return;
101:                            }
102:
103:                            long d = maxObjectInspectionAge();
104:                            if (d >= l) {
105:                                /* Do a full collection.  There is a remote possibility
106:                                 * that a full collection will occurr between the time
107:                                 * we sample the inspection age and the time the GC
108:                                 * actually starts, but this is sufficiently unlikely
109:                                 * that it doesn't seem worth the more expensive JVM
110:                                 * interface that would be required.
111:                                 */
112:                                System.gc();
113:                                d = 0;
114:                            }
115:
116:                            /* Wait for the latency period to expire,
117:                             * or for notification that the period has changed
118:                             */
119:                            try {
120:                                lock.wait(l - d);
121:                            } catch (InterruptedException x) {
122:                                continue;
123:                            }
124:                        }
125:                    }
126:                }
127:
128:                private Daemon(ThreadGroup tg) {
129:                    super (tg, "GC Daemon");
130:                }
131:
132:                /* Create a new daemon thread in the root thread group */
133:                public static void create() {
134:                    PrivilegedAction pa = new PrivilegedAction() {
135:                        public Object run() {
136:                            ThreadGroup tg = Thread.currentThread()
137:                                    .getThreadGroup();
138:                            for (ThreadGroup tgn = tg; tgn != null; tg = tgn, tgn = tg
139:                                    .getParent())
140:                                ;
141:                            Daemon d = new Daemon(tg);
142:                            d.setDaemon(true);
143:                            d.setPriority(Thread.MIN_PRIORITY + 1);
144:                            d.start();
145:                            GC.daemon = d;
146:                            return null;
147:                        }
148:                    };
149:                    AccessController.doPrivileged(pa);
150:                }
151:
152:            }
153:
154:            /* Sets the latency target to the given value.
155:             * Must be invoked while holding the lock.
156:             */
157:            private static void setLatencyTarget(long ms) {
158:                latencyTarget = ms;
159:                if (daemon == null) {
160:                    /* Create a new daemon thread */
161:                    Daemon.create();
162:                } else {
163:                    /* Notify the existing daemon thread
164:                     * that the lateency target has changed
165:                     */
166:                    lock.notify();
167:                }
168:            }
169:
170:            /**
171:             * Represents an active garbage-collection latency request.  Instances of
172:             * this class are created by the <code>{@link #requestLatency}</code>
173:             * method.  Given a request, the only interesting operation is that of
174:             * cancellation.
175:             */
176:            public static class LatencyRequest implements  Comparable {
177:
178:                /* Instance counter, used to generate unique identifers */
179:                private static long counter = 0;
180:
181:                /* Sorted set of active latency requests */
182:                private static SortedSet requests = null;
183:
184:                /* Examine the request set and reset the latency target if necessary.
185:                 * Must be invoked while holding the lock.
186:                 */
187:                private static void adjustLatencyIfNeeded() {
188:                    if ((requests == null) || requests.isEmpty()) {
189:                        if (latencyTarget != NO_TARGET) {
190:                            setLatencyTarget(NO_TARGET);
191:                        }
192:                    } else {
193:                        LatencyRequest r = (LatencyRequest) requests.first();
194:                        if (r.latency != latencyTarget) {
195:                            setLatencyTarget(r.latency);
196:                        }
197:                    }
198:                }
199:
200:                /* The requested latency, or NO_TARGET
201:                 * if this request has been cancelled
202:                 */
203:                private long latency;
204:
205:                /* Unique identifier for this request */
206:                private long id;
207:
208:                private LatencyRequest(long ms) {
209:                    if (ms <= 0) {
210:                        throw new IllegalArgumentException(
211:                                "Non-positive latency: " + ms);
212:                    }
213:                    this .latency = ms;
214:                    synchronized (lock) {
215:                        this .id = ++counter;
216:                        if (requests == null) {
217:                            requests = new TreeSet();
218:                        }
219:                        requests.add(this );
220:                        adjustLatencyIfNeeded();
221:                    }
222:                }
223:
224:                /**
225:                 * Cancels this latency request.
226:                 *
227:                 * @throws  IllegalStateException
228:                 *          If this request has already been cancelled
229:                 */
230:                public void cancel() {
231:                    synchronized (lock) {
232:                        if (this .latency == NO_TARGET) {
233:                            throw new IllegalStateException("Request already"
234:                                    + " cancelled");
235:                        }
236:                        if (!requests.remove(this )) {
237:                            throw new InternalError("Latency request " + this 
238:                                    + " not found");
239:                        }
240:                        if (requests.isEmpty())
241:                            requests = null;
242:                        this .latency = NO_TARGET;
243:                        adjustLatencyIfNeeded();
244:                    }
245:                }
246:
247:                public int compareTo(Object o) {
248:                    LatencyRequest r = (LatencyRequest) o;
249:                    long d = this .latency - r.latency;
250:                    if (d == 0)
251:                        d = this .id - r.id;
252:                    return (d < 0) ? -1 : ((d > 0) ? +1 : 0);
253:                }
254:
255:                public String toString() {
256:                    return (LatencyRequest.class.getName() + "[" + latency
257:                            + "," + id + "]");
258:                }
259:
260:            }
261:
262:            /**
263:             * Makes a new request for a garbage-collection latency of the given
264:             * number of real-time milliseconds.  A low-priority daemon thread makes a
265:             * best effort to ensure that the maximum object-inspection age never
266:             * exceeds the smallest of the currently active requests.
267:             *
268:             * @param   latency
269:             *          The requested latency
270:             * 
271:             * @throws  IllegalArgumentException
272:             *          If the given <code>latency</code> is non-positive
273:             */
274:            public static LatencyRequest requestLatency(long latency) {
275:                return new LatencyRequest(latency);
276:            }
277:
278:            /**
279:             * Returns the current smallest garbage-collection latency request, or zero
280:             * if there are no active requests.
281:             */
282:            public static long currentLatencyTarget() {
283:                long t = latencyTarget;
284:                return (t == NO_TARGET) ? 0 : t;
285:            }
286:
287:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.