FileDocCategorySizeDatePackage
FrequencyLimitedDispatcher.javaAPI DocAzureus 3.0.3.42118Sat Aug 19 07:13:58 BST 2006org.gudy.azureus2.core3.util

FrequencyLimitedDispatcher

public class FrequencyLimitedDispatcher extends Object

Fields Summary
private AERunnable
target
private long
min_millis
private long
last_run
private DelayedEvent
delay_event
Constructors Summary
public FrequencyLimitedDispatcher(AERunnable _target, int _min_frequency_millis)

		target		= _target;
		min_millis	= _min_frequency_millis;
	
Methods Summary
public voiddispatch()

		long	now = SystemTime.getCurrentTime();
		
		boolean	run_it	= false;
		
		synchronized( this ){
			
			long	delay = min_millis - (now - last_run );

			if ( now < last_run || delay <= 0 ){
				
				last_run	= now;
				
				run_it		= true;
				
			}else{
				
					// run too recently
				
				if ( delay_event == null ){
											
					delay_event = 
						new DelayedEvent(
							"FreqLimDisp",
							delay,
							new AERunnable()
							{
								public void
								runSupport()
								{
									long	now = SystemTime.getCurrentTime();

									synchronized( this ){

										last_run = now;
										
										delay_event	= null;
									}
									
									target.run();
								}
							});
				}
			}
		}
		
		if ( run_it ){
			
			target.run();
		}