1 package com.flexiblewebsolutions.xdriveunit.loadtest; 2 3 import java.util.ArrayList; 4 5 import org.apache.log4j.Logger; 6 7 /*** 8 * Executes logic when all threads have completed. 9 * 10 * @author Donavon Buss 11 */ 12 public class ThreadTracker 13 { 14 static Logger logger = Logger.getLogger( ThreadTracker.class.getName() ); 15 private ArrayList _RunningThreads = new ArrayList(); 16 17 private ArrayList _ThreadNames = new ArrayList(); 18 19 private int _Registered = 0; 20 21 private int _NumToRegister = 0; 22 23 private ThreadCallback _Traceback = null; 24 25 /*** 26 * Record this thread as in process 27 * 28 * @param pThreadName - 29 * Unique name for this thread 30 */ 31 public void registerThread( String pThreadName ) 32 { 33 logger.debug( "registerThread- " + pThreadName ); 34 if( !_RunningThreads.contains( pThreadName ) ) 35 { 36 _RunningThreads.add( pThreadName ); 37 _ThreadNames.add( pThreadName ); 38 _Registered++; 39 } 40 } 41 42 /*** 43 * Record this thread as finished 44 * 45 * @param pThreadName - 46 * Unique name for thread. 47 */ 48 public void deregisterThread( String pThreadName ) 49 { 50 logger.debug( "deregisterThread- " + pThreadName ); 51 _RunningThreads.remove( pThreadName ); 52 if( _Registered == _NumToRegister && ( _RunningThreads.size() == 0 ) ) 53 { 54 finishProcessing(); 55 } 56 } 57 58 /*** 59 * Build resultsSummary.xml and generate reports. 60 */ 61 private void finishProcessing() 62 { 63 _Traceback.threadsFinished( _ThreadNames ); 64 65 logger.info( "All Threads Complete." ); 66 } 67 68 /*** 69 * Sets number of parameters that will be registered 70 * 71 * @param pNumToRegister - 72 * Number of threads that should register. 73 * @param pTT - 74 * Thread Traceback object that will be invoked when all threads 75 * have completed. 76 */ 77 public ThreadTracker( int pNumToRegister, ThreadCallback pTT ) 78 { 79 super(); 80 _NumToRegister = pNumToRegister; 81 _Traceback = pTT; 82 } 83 }