Methods Summary |
---|
private synchronized void | finishCountNotify()Notify threads they can exit
finished = true;
notifyAll();
|
private synchronized void | finishCountWait()Wait until all threads are finished
finished_thread_count++;
while(!finished) {
try { wait(); } catch(InterruptedException ie) {}
}
|
public static void | main(java.lang.String[] args)Run a test with a count of threads and pool size specified at
the command line.
int nthreads = DEFAULT_THREAD_COUNT;
int poolsize = DEFAULT_POOL_SIZE;
if(args.length > 0) {
nthreads = Integer.parseInt(args[0]);
}
if(args.length > 1) {
poolsize = Integer.parseInt(args[1]);
}
if(nthreads <= 0 || poolsize <= 0) {
System.out.println("Usage: PoolTest <number_of_threads>");
System.exit(-1);
}
System.out.println("Running test with " + nthreads + " threads and " +
poolsize + " entries in the pool");
PoolTest pt = new PoolTest(nthreads, poolsize);
|
public void | run()Run each individual thread
// wait until we are allowed to start
startCountWait();
try {
// get the parser
DocumentBuilder db = (DocumentBuilder) pool.getResource();
//DocumentBuilder db = dbf.newDocumentBuilder();
// parse
if(!finished) {
db.parse(new InputSource(new FileReader("long.xml")));
}
// return the resource
pool.returnResource(db);
} catch(Throwable t) {
System.out.println("Error " + t.getMessage() +
" encountered: terminating\n");
had_error = true;
finishCountNotify();
} finally {
finishCountWait();
}
|
private long | runPool(int nthreads, int poolsize)Run a pool with a given number of threads and pool size. Return
the amount of time it took
// create the pool or builder
pool = new ResourcePool(new XMLParserFactory(), poolsize);
//dbf = DocumentBuilderFactory.newInstance();
// setup counters
max_thread_count = nthreads;
finished_thread_count = 0;
finished = false;
had_error = false;
ThreadGroup group = new ThreadGroup("PoolTest");
// create threads
for (started_thread_count = 0;
started_thread_count < max_thread_count;
started_thread_count++) {
(new Thread(group, this)).start();
}
long startTime = System.currentTimeMillis();
// and we're off! Start all threads at once
startCountNotify();
// wait until all threads have finished
while(finished_thread_count < max_thread_count) {
Thread.yield();
}
long totalTime = System.currentTimeMillis() - startTime;
// allow threads to exit all at once
finishCountNotify();
// clean up
pool.destroy();
if(had_error)
return -1;
return totalTime;
|
private synchronized void | startCountNotify()Notify all threads we are starting
notifyAll();
|
private synchronized void | startCountWait()Wait until all threads are ready to start
while(started_thread_count < max_thread_count) {
try { wait(); } catch(InterruptedException ie) {}
}
|