FileDocCategorySizeDatePackage
Timer.javaAPI DocAzureus 3.0.3.49418Wed Nov 29 09:06:32 GMT 2006org.gudy.azureus2.core3.util

Timer

public class Timer extends AERunnable implements SystemTime.consumer
author
parg

Fields Summary
private static boolean
DEBUG_TIMERS
private static ArrayList
timers
private static AEMonitor
timers_mon
private ThreadPool
thread_pool
private Set
events
private long
unique_id_next
private volatile boolean
destroyed
private boolean
indestructable
private boolean
log
private int
max_events_logged
Constructors Summary
public Timer(String name)

	
	
	
			 
	
		this( name, 1 );
	
public Timer(String name, int thread_pool_size)

		this(name, thread_pool_size, Thread.NORM_PRIORITY);
	
public Timer(String name, int thread_pool_size, int thread_priority)

		if (DEBUG_TIMERS) {
			try {
				timers_mon.enter();
				if (timers == null) {
					timers = new ArrayList();
					AEDiagnostics.addEvidenceGenerator(new evidenceGenerator()); 
				}
				timers.add(new WeakReference(this));
			} finally {
				timers_mon.exit();
			}
		}

		thread_pool = new ThreadPool(name,thread_pool_size);
	
		SystemTime.registerClockChangeListener( this );

		Thread t = new Thread(this, "Timer:" + name );
		
		t.setDaemon( true );
		
		t.setPriority(thread_priority);
			
		t.start();
	
Methods Summary
public synchronized TimerEventaddEvent(long when, TimerEventPerformer performer)

		return( addEvent( SystemTime.getCurrentTime(), when, performer ));
	
public synchronized TimerEventaddEvent(java.lang.String name, long when, TimerEventPerformer performer)

		return( addEvent( name, SystemTime.getCurrentTime(), when, performer ));
	
public synchronized TimerEventaddEvent(long creation_time, long when, TimerEventPerformer performer)

		return( addEvent( null, creation_time, when, performer ));
	
public synchronized TimerEventaddEvent(java.lang.String name, long creation_time, long when, TimerEventPerformer performer)

		TimerEvent	event = new TimerEvent( this, unique_id_next++, creation_time, when, performer );
		
		if ( name != null ){
			
			event.setName( name );
		}
		
		events.add( event );
		
		if ( log ){
			
			if ( !(performer instanceof TimerEventPerformer )){
				
				System.out.println( "Timer '" + thread_pool.getName() + "' - added " + event.getString());
			}
					
			if ( events.size() > max_events_logged ){
		
				max_events_logged = events.size();
				
				System.out.println( "Timer '" + thread_pool.getName() + "' - events = " + max_events_logged );
			}
		}
		
		// System.out.println( "event added (" + when + ") - queue = " + events.size());
				
		notify();
		
		return( event );
	
public synchronized TimerEventPeriodicaddPeriodicEvent(long frequency, TimerEventPerformer performer)

		return( addPeriodicEvent( null, frequency, performer ));
	
public synchronized TimerEventPeriodicaddPeriodicEvent(java.lang.String name, long frequency, TimerEventPerformer performer)

		TimerEventPeriodic periodic_performer = new TimerEventPeriodic( this, frequency, performer );
		
		if ( name != null ){
			
			periodic_performer.setName( name );
		}
		
		if ( log ){
						
			System.out.println( "Timer '" + thread_pool.getName() + "' - added " + periodic_performer.getString());
		}
		
		return( periodic_performer );
	
public voidadjustAllBy(long offset)

		// fix up the timers

		synchronized (this) {

			// as we're adjusting all events by the same amount the ordering remains valid

			Iterator it = events.iterator();

			while (it.hasNext()) {

				TimerEvent event = (TimerEvent) it.next();

				long old_when = event.getWhen();
				long new_when = old_when + offset;

				// System.out.println( "    adjusted: " + old_when + " -> " + new_when );

				event.setWhen(new_when);
			}

			notify();
		}
	
protected synchronized voidcancelEvent(TimerEvent event)

		if ( events.contains( event )){
			
			events.remove( event );
		
			// System.out.println( "event cancelled (" + event.getWhen() + ") - queue = " + events.size());
	
			notify();
		}
	
public voidconsume(long offset)

		// System.out.println( "Timer '" + thread_pool.getName() +"': clock change by " + offset );
		  
		if ( Math.abs( offset ) >= 60*1000 ){
			
				// fix up the timers
			
			synchronized( this ){
				
					// as we're adjusting all events by the same amount the ordering remains valid
				
				Iterator	it = events.iterator();
				
				while (it.hasNext()){
					
					TimerEvent	event = (TimerEvent)it.next();
					
					long	old_when = event.getWhen();
					long	new_when = old_when + offset;
					
					// System.out.println( "    adjusted: " + old_when + " -> " + new_when );
					
					event.setWhen( new_when );
				}

				notify();
			}
		}
	
public synchronized voiddestroy()

		if ( indestructable ){
			
			Debug.out( "Attempt to destroy indestructable timer '" + getName() + "'" );
			
		}else{
			
			destroyed	= true;
			
			notify();
			
			SystemTime.unregisterClockChangeListener( this );
		}

		if (DEBUG_TIMERS) {
			try {
				timers_mon.enter();
				// crappy
				for (Iterator iter = timers.iterator(); iter.hasNext();) {
					WeakReference timerRef = (WeakReference) iter.next();
					Object timer = timerRef.get();
					if (timer == null || timer == this) {
						iter.remove();
					}
				}
			} finally {
				timers_mon.exit();
			}
		}
	
public synchronized voiddump()

		System.out.println( "Timer '" + thread_pool.getName() + "': dump" );

		Iterator	it = events.iterator();
		
		while(it.hasNext()){
			
			TimerEvent	ev = (TimerEvent)it.next();
			
			System.out.println( "\t" + ev.getString());
		}
	
public booleangetLogging()

		return log;
	
public java.lang.StringgetName()

		return( thread_pool.getName());
	
public voidrunSupport()

		while( true ){
			
			try{
				TimerEvent	event_to_run = null;
				
				synchronized(this){
					
					if ( destroyed ){
						
						break;
					}
					
					if ( events.isEmpty()){
						
						// System.out.println( "waiting forever" );
						
						this.wait();
						
					}else{
						long	now = SystemTime.getCurrentTime();
						
						TimerEvent	next_event = (TimerEvent)events.iterator().next();
						
						long	delay = next_event.getWhen() - now;
						
						if ( delay > 0 ){
							
							// System.out.println( "waiting for " + delay );
							
							this.wait(delay);
						}
					}
				
					if ( destroyed ){
						
						break;
					}
					
					long	now = SystemTime.getCurrentTime();
					
					Iterator	it = events.iterator();
					
					while( it.hasNext()){
						
						TimerEvent	event = (TimerEvent)it.next();
						
						if ( event.getWhen() <= now ){
							
							event_to_run = event;
							
							it.remove();
							
							break;
						}
					}
				}
				
				if ( event_to_run != null ){
					
					event_to_run.setHasRun();
					
					if (log) {
						System.out.println( "running: " + event_to_run.getString() );
					}
					
					thread_pool.run(event_to_run.getRunnable());
				}
				
			}catch( Throwable e ){
				
				Debug.printStackTrace( e );
			}
		}
	
public voidsetIndestructable()

		indestructable	= true;
	
public voidsetLogging(boolean _log)

		log	= _log;
	
public voidsetWarnWhenFull()

		thread_pool.setWarnWhenFull();