1 package net.javacoding.jspider.core.throttle.impl; 2 3 4 import net.javacoding.jspider.core.throttle.Throttle; 5 6 7 15 public class DistributedLoadThrottleImpl implements Throttle { 16 17 18 protected int milliseconds; 19 20 21 protected long lastAllow; 22 23 28 public DistributedLoadThrottleImpl(int milliseconds) { 29 this.milliseconds = milliseconds; 30 lastAllow = System.currentTimeMillis() - milliseconds; 31 } 32 33 37 public synchronized void throttle() { 38 39 long thisTime = System.currentTimeMillis(); 40 long scheduledTime = lastAllow + milliseconds; 41 42 if (scheduledTime > thisTime) { 43 try { 44 Thread.sleep(scheduledTime - thisTime); 45 } catch (InterruptedException e) { 46 Thread.currentThread().interrupt(); 47 } 48 } 49 50 lastAllow = System.currentTimeMillis(); 51 } 52 } 53 54 | Popular Tags |