Source Code Cross Referenced for IDEIdleHelper.java in  » IDE-Eclipse » ui-ide » org » eclipse » ui » internal » ide » application » 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 » IDE Eclipse » ui ide » org.eclipse.ui.internal.ide.application 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Copyright (c) 2005, 2007 IBM Corporation and others.
003:         * All rights reserved. This program and the accompanying materials
004:         * are made available under the terms of the Eclipse Public License v1.0
005:         * which accompanies this distribution, and is available at
006:         * http://www.eclipse.org/legal/epl-v10.html
007:         *
008:         * Contributors:
009:         *     IBM Corporation - initial API and implementation
010:         *******************************************************************************/package org.eclipse.ui.internal.ide.application;
011:
012:        import org.eclipse.core.runtime.IProgressMonitor;
013:        import org.eclipse.core.runtime.IStatus;
014:        import org.eclipse.core.runtime.Platform;
015:        import org.eclipse.core.runtime.Status;
016:        import org.eclipse.core.runtime.jobs.Job;
017:        import org.eclipse.swt.SWT;
018:        import org.eclipse.swt.SWTException;
019:        import org.eclipse.swt.widgets.Display;
020:        import org.eclipse.swt.widgets.Event;
021:        import org.eclipse.swt.widgets.Listener;
022:        import org.eclipse.ui.PlatformUI;
023:        import org.eclipse.ui.application.IWorkbenchConfigurer;
024:        import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
025:        import org.eclipse.ui.internal.ide.Policy;
026:
027:        /**
028:         * The idle helper detects when the system is idle in order to perform garbage 
029:         * collection in a way that minimizes impact on responsiveness of the UI.
030:         * The algorithm for determining when to perform a garbage collection
031:         * is as follows:
032:         * 
033:         *  - Never gc if there is a test harness present
034:         *  - Don't gc if background jobs are running
035:         *  - Don't gc if the keyboard or mouse have been active within IDLE_INTERVAL
036:         *  - Don't gc if there has been a GC within the minimum gc interval (system property PROP_GC_INTERVAL)
037:         *  - After a gc, don't gc again until (duration * GC_DELAY_MULTIPLIER) has elapsed.
038:         *    For example, if a GC takes 100ms and the multiplier is 60, don't gc for at least five seconds
039:         *  - Never gc again if any single gc takes longer than system property PROP_GC_MAX
040:         */
041:        class IDEIdleHelper {
042:
043:            /**
044:             * The default minimum time between garbage collections.
045:             */
046:            private static final int DEFAULT_GC_INTERVAL = 60000;
047:
048:            /**
049:             * The default maximum duration for a garbage collection, beyond which
050:             * the explicit gc mechanism is automatically disabled.
051:             */
052:            private static final int DEFAULT_GC_MAX = 8000;
053:
054:            /**
055:             * The multiple of the last gc duration before we will consider doing
056:             * another one.
057:             */
058:            private static final int GC_DELAY_MULTIPLIER = 60;
059:
060:            /**
061:             * The time interval of no keyboard or mouse events after which the system 
062:             * is considered idle.
063:             */
064:            private static final int IDLE_INTERVAL = 5000;
065:
066:            /**
067:             * The name of the boolean system property that specifies whether explicit
068:             * garbage collection is enabled.
069:             */
070:            private static final String PROP_GC = "ide.gc"; //$NON-NLS-1$
071:
072:            /**
073:             * The name of the integer system property that specifies the minimum time 
074:             * interval in milliseconds between garbage collections.
075:             */
076:            private static final String PROP_GC_INTERVAL = "ide.gc.interval"; //$NON-NLS-1$
077:
078:            /**
079:             * The name of the integer system property that specifies the maximum 
080:             * duration for a garbage collection. If this duration is ever exceeded, the 
081:             * explicit gc mechanism is disabled for the remainder of the session.
082:             */
083:            private static final String PROP_GC_MAX = "ide.gc.max"; //$NON-NLS-1$
084:
085:            protected IWorkbenchConfigurer configurer;
086:
087:            private Listener idleListener;
088:
089:            /**
090:             * The last time we garbage collected.
091:             */
092:            private long lastGC = System.currentTimeMillis();
093:
094:            /**
095:             * The maximum gc duration. If this value is exceeded, the
096:             * entire explicit gc mechanism is disabled.
097:             */
098:            private int maxGC = DEFAULT_GC_MAX;
099:            /**
100:             * The minimum time interval until the next garbage collection
101:             */
102:            private int minGCInterval = DEFAULT_GC_INTERVAL;
103:
104:            /**
105:             * The time interval until the next garbage collection
106:             */
107:            private int nextGCInterval = DEFAULT_GC_INTERVAL;
108:
109:            private Job gcJob;
110:
111:            private Runnable handler;
112:
113:            /**
114:             * Creates and initializes the idle handler
115:             * @param aConfigurer The workbench configurer.
116:             */
117:            IDEIdleHelper(IWorkbenchConfigurer aConfigurer) {
118:                this .configurer = aConfigurer;
119:                //don't gc while running tests because performance tests are sensitive to timing (see bug 121562)
120:                if (PlatformUI.getTestableObject().getTestHarness() != null) {
121:                    return;
122:                }
123:                String enabled = System.getProperty(PROP_GC);
124:                //gc is turned on by default if property is missing
125:                if (enabled != null
126:                        && enabled.equalsIgnoreCase(Boolean.FALSE.toString())) {
127:                    return;
128:                }
129:                //init gc interval
130:                Integer prop = Integer.getInteger(PROP_GC_INTERVAL);
131:                if (prop != null && prop.intValue() >= 0) {
132:                    minGCInterval = nextGCInterval = prop.intValue();
133:                }
134:
135:                //init max gc interval
136:                prop = Integer.getInteger(PROP_GC_MAX);
137:                if (prop != null) {
138:                    maxGC = prop.intValue();
139:                }
140:
141:                createGarbageCollectionJob();
142:
143:                //hook idle handler
144:                final Display display = configurer.getWorkbench().getDisplay();
145:                handler = new Runnable() {
146:                    public void run() {
147:                        if (!display.isDisposed()
148:                                && !configurer.getWorkbench().isClosing()) {
149:                            int nextInterval;
150:                            final long start = System.currentTimeMillis();
151:                            //don't garbage collect if background jobs are running
152:                            if (!Platform.getJobManager().isIdle()) {
153:                                nextInterval = IDLE_INTERVAL;
154:                            } else if ((start - lastGC) < nextGCInterval) {
155:                                //don't garbage collect if we have collected within the specific interval
156:                                nextInterval = nextGCInterval
157:                                        - (int) (start - lastGC);
158:                            } else {
159:                                gcJob.schedule();
160:                                nextInterval = minGCInterval;
161:                            }
162:                            display.timerExec(nextInterval, this );
163:                        }
164:                    }
165:                };
166:                idleListener = new Listener() {
167:                    public void handleEvent(Event event) {
168:                        display.timerExec(IDLE_INTERVAL, handler);
169:                    }
170:                };
171:                display.addFilter(SWT.KeyUp, idleListener);
172:                display.addFilter(SWT.MouseUp, idleListener);
173:            }
174:
175:            /**
176:             * Creates the job that performs garbage collection
177:             */
178:            private void createGarbageCollectionJob() {
179:                gcJob = new Job(IDEWorkbenchMessages.IDEIdleHelper_backgroundGC) {
180:                    protected IStatus run(IProgressMonitor monitor) {
181:                        final Display display = configurer.getWorkbench()
182:                                .getDisplay();
183:                        if (display != null && !display.isDisposed()) {
184:                            final long start = System.currentTimeMillis();
185:                            System.gc();
186:                            System.runFinalization();
187:                            lastGC = start;
188:                            final int duration = (int) (System
189:                                    .currentTimeMillis() - start);
190:                            if (Policy.DEBUG_GC) {
191:                                System.out
192:                                        .println("Explicit GC took: " + duration); //$NON-NLS-1$
193:                            }
194:                            if (duration > maxGC) {
195:                                if (Policy.DEBUG_GC) {
196:                                    System.out
197:                                            .println("Further explicit GCs disabled due to long GC"); //$NON-NLS-1$
198:                                }
199:                                shutdown();
200:                            } else {
201:                                //if the gc took a long time, ensure the next gc doesn't happen for awhile
202:                                nextGCInterval = Math.max(minGCInterval,
203:                                        GC_DELAY_MULTIPLIER * duration);
204:                                if (Policy.DEBUG_GC) {
205:                                    System.out
206:                                            .println("Next GC to run in: " + nextGCInterval); //$NON-NLS-1$
207:                                }
208:                            }
209:                        }
210:                        return Status.OK_STATUS;
211:                    }
212:                };
213:                gcJob.setSystem(true);
214:            }
215:
216:            /**
217:             * Shuts down the idle helper, removing any installed listeners, etc.
218:             */
219:            void shutdown() {
220:                if (idleListener == null) {
221:                    return;
222:                }
223:                final Display display = configurer.getWorkbench().getDisplay();
224:                if (display != null && !display.isDisposed()) {
225:                    try {
226:                        display.asyncExec(new Runnable() {
227:                            public void run() {
228:                                display.timerExec(-1, handler);
229:                                display.removeFilter(SWT.KeyUp, idleListener);
230:                                display.removeFilter(SWT.MouseUp, idleListener);
231:                            }
232:                        });
233:                    } catch (SWTException ex) {
234:                        // ignore (display might be disposed)
235:                    }
236:                }
237:            }
238:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.