1 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 e) { 46 } 48 } else { 49 Thread.yield(); 50 } 51 } else { 52 fLastCalled= System.currentTimeMillis(); 53 } 54 } 55 } 56 | Popular Tags |