1 19 package org.apache.avalon.excalibur.component.example_im; 20 21 import java.io.BufferedReader ; 22 import java.io.File ; 23 import java.io.InputStreamReader ; 24 25 import org.apache.avalon.excalibur.component.ExcaliburComponentManagerCreator; 26 import org.apache.avalon.framework.service.ServiceManager; 27 28 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier; 29 30 31 43 public class Main 44 { 45 private static ExcaliburComponentManagerCreator m_componentManagerCreator; 46 47 50 private Main() 51 { 52 } 53 54 57 60 public static void main( String [] args ) 61 throws Exception 62 { 63 System.out.println( "Running the InstrumentManager Example Application" ); 64 65 m_componentManagerCreator = new ExcaliburComponentManagerCreator( null, 69 new File ( "../conf/logkit.xml" ), new File ( "../conf/roles.xml" ), 70 new File ( "../conf/components.xml" ), new File ( "../conf/instrument.xml" ) ); 71 72 ServiceManager serviceManager = m_componentManagerCreator.getServiceManager(); 74 75 ExampleInstrumentable instrumentable = 77 (ExampleInstrumentable)serviceManager.lookup( ExampleInstrumentable.ROLE ); 78 try 79 { 80 boolean quit = false; 81 while( !quit ) 82 { 83 System.out.println( "Enter the number of times that exampleAction should be " 84 + "called, or 'q' to quit." ); 85 BufferedReader in = new BufferedReader ( new InputStreamReader ( System.in ) ); 86 System.out.print( " : " ); 87 String cntStr = in.readLine(); 88 89 if( ( cntStr == null ) || ( cntStr.equalsIgnoreCase( "q" ) ) ) 91 { 92 quit = true; 93 } 94 else if( ( cntStr.equalsIgnoreCase( "gc" ) ) ) 95 { 96 System.gc(); 97 } 98 else 99 { 100 try 101 { 102 int concurrent = 100; 103 CyclicBarrier barrier = new CyclicBarrier( concurrent ); 104 int cnt = Integer.parseInt( cntStr ); 105 int average = Math.max( cnt / concurrent, 1 ); 106 107 while( cnt > 0 ) 108 { 109 Thread t = new Thread ( new ActionRunner( instrumentable, 110 Math.min( average, cnt ), 111 barrier ) ); 112 t.start(); 113 114 if( cnt > 0 ) 115 { 116 cnt -= average; 117 } 118 119 if( cnt < 0 ) 120 { 121 cnt = 0; 122 } 123 } 124 } 125 catch( NumberFormatException e ) 126 { 127 } 128 } 129 } 130 } 131 finally 132 { 133 serviceManager.release( instrumentable ); 135 instrumentable = null; 136 137 m_componentManagerCreator.dispose(); 140 } 141 142 System.out.println(); 143 System.out.println( "Exiting..." ); 144 System.exit( 0 ); 145 } 146 147 private static final class ActionRunner implements Runnable 148 { 149 private final int m_numIterations; 150 private final ExampleInstrumentable m_instrumentable; 151 private final CyclicBarrier m_barrier; 152 153 protected ActionRunner( ExampleInstrumentable instrumentable, int numIterations, CyclicBarrier barrier ) 154 { 155 m_numIterations = numIterations; 156 m_instrumentable = instrumentable; 157 m_barrier = barrier; 158 } 159 160 public void run() 161 { 162 for( int i = 0; i < m_numIterations; i++ ) 163 { 164 m_instrumentable.doAction(); 165 } 166 167 try 168 { 169 m_barrier.barrier(); 170 } 171 catch( Exception e ) 172 { 173 } 174 } 175 } 176 } 177 178 | Popular Tags |