Methods Summary |
---|
static synchronized void | decThreads()numThreads--;
|
static int | getNumThreads()return numThreads;
|
static synchronized void | incThreads()numThreads++;
|
public static void | main(java.lang.String[] args)
int tmpREPEAT = (args.length > 0) ? Integer.parseInt(args[0]) : 10000000;
if (args.length < 2)
{
//don't use this if we have two or more args
REPEAT = 10000;
test();
}
REPEAT = tmpREPEAT;
test();
|
public void | run()
Counter1.initialize(0);
waitForGo("Counter1");
for (int i = REPEAT; i > 0; i--)
Counter1.addAmount(0, 1);
stop("Counter1 count: " + Counter1.getAmount(0));
Counter2.initialize(0);
waitForGo("Counter2");
for (int i = REPEAT; i > 0; i--)
Counter2.addAmount(0, 1);
stop("Counter2 count: " + Counter2.getAmount(0));
Counter3.initialize(this);
waitForGo("Counter3");
for (int i = REPEAT; i > 0; i--)
Counter3.addAmount(0, 1);
stop("Counter3 count: " + Counter3.getAmount(0));
|
public static void | runOneTest()
//wait until all threads are ready to run
while(getNumThreads() < NUMBER_OF_THREADS)
try{Thread.sleep(10);}catch(InterruptedException e){}
//tell the threads to go
synchronized (LOCK1) {LOCK1.notifyAll();}
//wait for them all to stop
while(getNumThreads() > 0)
try{Thread.sleep(100);}catch(InterruptedException e){}
//reset the start time
startTime = -1;
//All threads have stopped now, so we can release them
synchronized (LOCK2) {LOCK2.notifyAll();}
|
public static synchronized void | setStartTime()
//only sets the start time once
if (startTime == -1)
startTime = System.currentTimeMillis();
|
public static void | stop(java.lang.String mess)
//print time and count for every thread.
//Only the last thread to terminate gives full values.
System.out.println(mess + " time: " + (System.currentTimeMillis()-startTime));
decThreads();
//Wait until the main threads knows that we've finished
synchronized(LOCK2)
{
try {LOCK2.wait();}
catch (InterruptedException e) {}
}
|
public static void | test()
for (int i = 0; i < NUMBER_OF_THREADS; i++)
(new CounterTest(i)).start();
runOneTest();
runOneTest();
runOneTest();
|
public static void | waitForGo(java.lang.String mess)
//Register that we are ready to run
incThreads();
//wait until told to start, so that we can start all
//the threads at approximately the same time.
synchronized(LOCK1)
{
try {LOCK1.wait();}
catch (InterruptedException e) {}
}
System.out.println("Starting test for " + mess);
//Set the start time only once at the beginning
//of the first thread to start.
setStartTime();
|