FileDocCategorySizeDatePackage
NonDaemonTaskRunner.javaAPI DocAzureus 3.0.3.44739Fri Jul 29 12:25:36 BST 2005org.gudy.azureus2.core3.util

NonDaemonTaskRunner

public class NonDaemonTaskRunner extends Object
author
parg

Fields Summary
public static final int
LINGER_PERIOD
protected static NonDaemonTaskRunner
singleton
protected static AEMonitor
class_mon
protected Stack
tasks
protected AEMonitor
tasks_mon
protected AESemaphore
task_sem
protected List
wait_until_idle_list
protected Thread
current_thread
Constructors Summary
Methods Summary
protected static org.gudy.azureus2.core3.util.NonDaemonTaskRunnergetSingleton()

	
	  
	
	
		try{
			class_mon.enter();
		
			if ( singleton == null ){
				
				singleton = new NonDaemonTaskRunner();
			}
			
			return( singleton );

		}finally{
		
			class_mon.exit();
		}
	
public static java.lang.Objectrun(NonDaemonTask target)

		return(getSingleton().runSupport( target, false ));
	
public static java.lang.ObjectrunAsync(NonDaemonTask target)

		return(getSingleton().runSupport( target, true ));
	
protected java.lang.ObjectrunSupport(NonDaemonTask target, boolean async)

			// is this a recursive call? if so, run directly
		
		if ( current_thread == Thread.currentThread()){
			
			return( target.run());
		}
		
		taskWrapper	wrapper = new taskWrapper( target );
		
		try{
			tasks_mon.enter();
			
			tasks.push( wrapper );
			
			task_sem.release();
			
			if ( current_thread == null ){
				
				final AESemaphore wait_sem = new AESemaphore("NonDaemonTaskRunnerTask");
				
				current_thread = new AEThread("NonDaemonTaskRunner" )
					{
						public void
						runSupport()
						{
							wait_sem.release();
							
							// System.out.println( "non daemon starts" );
							
							while(true){
								
								task_sem.reserve(LINGER_PERIOD);
																
								taskWrapper t			= null;
								
								try{
									tasks_mon.enter();
									
									if ( tasks.isEmpty()){
	
										current_thread = null;
											
										for (int i=0;i<wait_until_idle_list.size();i++){
											
											((AESemaphore)wait_until_idle_list.get(i)).release();
										}
										
										wait_until_idle_list.clear();
										
										break;
										
									}else{
										
										t = (taskWrapper)tasks.pop();
									}
								}finally{
									
									tasks_mon.exit();
								}
								
								t.run();
							}
													
							// System.out.println( "non daemon ends" );
						}
					};
						
				current_thread.setDaemon(false);
				
				current_thread.start();	
				
				wait_sem.reserve();
			}
		}finally{
			
			tasks_mon.exit();
		}
		
		if ( async ){
			
			return( null );
		}
		
		return( wrapper.waitForResult());
	
public static voidwaitUntilIdle()

		getSingleton().waitUntilIdleSupport();
	
protected voidwaitUntilIdleSupport()

		AESemaphore	sem;
		
		try{
			tasks_mon.enter();
			
			if ( current_thread == null ){
				
				return;
			}
			
			sem = new AESemaphore("NDTR::idleWaiter");
			
			wait_until_idle_list.add( sem );
			
		}finally{
			
			tasks_mon.exit();
		}
		
		sem.reserve();