FileDocCategorySizeDatePackage
SiteTimer.javaAPI DocExample1575Sat Apr 23 22:35:36 BST 2005None

SiteTimer

public class SiteTimer extends Object

Fields Summary
CyclicBarrier
barrier
List
results
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

		new SiteTimer().start( args );
	
voidshowResults()

		Collections.sort( results );
		for( Result result : results )
			System.out.printf( "%-30.30s : %d\n", result.site, result.time );
		System.out.println("------------------");
	
public voidstart(java.lang.String[] args)

		Runnable showResultsAction = new Runnable() { 
			public void run() { 
				showResults(); 
				results.clear();
			} };
		barrier = new CyclicBarrier( args.length, showResultsAction );

		for ( final String site : args )
			new Thread() {
				public void run() {
					while( true ) {
						long time = timeConnect( site );
						results.add( new Result( time, site ) );
						try {
							barrier.await();
						} catch ( BrokenBarrierException e ) { return; 
						} catch ( InterruptedException e ) { return; }
					}
				}
			}.start();
	
static longtimeConnect(java.lang.String site)

		long start = System.currentTimeMillis();
		try { 
			new URL( site ).openConnection().connect(); 
		} catch ( IOException e ) { 
			return -1;
		}
		return System.currentTimeMillis() - start;