KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search2 > internal > ui > ThrottlingProgressMonitor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.search2.internal.ui;
12
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.core.runtime.ProgressMonitorWrapper;
15
16 public class ThrottlingProgressMonitor extends ProgressMonitorWrapper {
17     private float fThrottleRatio;
18     private long fLastCalled;
19     private long fSubMilis;
20
21     public ThrottlingProgressMonitor(IProgressMonitor wrapped, float throttleRatio) {
22         super(wrapped);
23         fThrottleRatio= throttleRatio;
24         fSubMilis= 0;
25         fLastCalled= 0;
26     }
27
28     public void internalWorked(double work) {
29         super.internalWorked(work);
30         if (fLastCalled != 0) {
31             long sleepTime= System.currentTimeMillis()-fLastCalled;
32             sleepTime *= fThrottleRatio;
33             sleepTime= Math.min(100, sleepTime);
34             if (sleepTime < 1) {
35                 fSubMilis++;
36                 if (fSubMilis > 50) {
37                     sleepTime= 1;
38                     fSubMilis= 0;
39                 }
40             }
41             fLastCalled= System.currentTimeMillis();
42             if (sleepTime > 0) {
43                 try {
44                     Thread.sleep(sleepTime);
45                 } catch (InterruptedException JavaDoc e) {
46                     // ignore
47
}
48             } else {
49                 Thread.yield();
50             }
51         } else {
52             fLastCalled= System.currentTimeMillis();
53         }
54     }
55 }
56
Popular Tags