Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Threads, performance and static classes

Status
Not open for further replies.

alexreuter

Programmer
Oct 11, 2001
6
US
Hello,
This is my first post here.
We have a static engine which returns reports, and I am working with a multi threaded testing application in an effort to test its scalability.
We are running inside java 1.1.8 on Solaris 7, with dual 4?? mhz processors and a couple gigs of ram. Here's my question:

The static engine has an Engine.load() method which initializes a bunch of instance data, xml files, hashtables..etc. When I simulate 4 concurrent requests to this engine I load the engine 2 ways.
1. Inside the main method, before I kick off the 4 requests(using threads obviously).
2. Inside the goTest() method, which is called by run().

When Engine.load() is called inside the goTest() method, it is called 4 times. This would seem to be a waste of time. I didn't notice this until we ramped up the number of requests and load time slowed to a crawl. So thats when I moved the Engine.load() call out to the main method, loading before sending off the requests. Heres the weird behavior:

After loading the 2nd way (with all the extra loads), reports, that is requests which use the static engine which was just loaded, perform 2-3 times FASTER?!? than if it is loaded a single time inside the main method.

Does anyone know why this might be?

I know for a fact that there are not multiple instances of the Engine being created because I unloaded() it with the last request as an experiment and all the instance data was wiped and all the requests failed.

So to summarize, why would mulitple loading of a static class improve performance by 200-300%?

TIA for any replies,
alex reuter
 
They are all running under the same JVM right? Could we possibly see some code. I know I can't give any real input from this description without some code.
 
OK. Here is the main method, with the Engine.load() call in it:
/////////////////////////////////////////
public static void main(String[] args) {
int numThreads = new Integer(args[0]).parseInt(args[0]);
System.out.println(numThreads);
try {
Engine.load();
} catch ( Exception e) {
e.printStackTrace();
}
for(int j=0; j<numThreads; j++){
ThreadedEngineTester trct = new ThreadedEngineTester();
trct.start();
}//for main thread loop
}
}

/////////////////////////////////////////
run() calls goTest() which counts the number of reports to run and runs them in this forloop
/////////////////////////////////////////

for(int i = 0; i < its; i++ ) {
Vector report = new Vector();
report.addElement(getReportNames().elementAt(i));
String rprt = (String)report.elementAt(0);
fos = new FileOutputStream(&quot;/tmp/report/&quot; + rprt + &quot;.xml&quot;);
start = System.currentTimeMillis();
try {
xmlReport = Engine.getReport(report);
} catch (Exception e) {
e.printStackTrace();
}
end = System.currentTimeMillis();

String timing = &quot;Report name: &quot;+ rprt+&quot; time (ms) = &quot; + (end-start) + &quot; ---------\n&quot;;
String xml = xmlReport.toString();
((XMLDocument)xmlReport).print(fos);
}

}

//////////////////////////////////////////////
I've trimmed out some stuff, but there's the meat of the calls.

Now the 2nd way we do it, the main method looks like this:
/////////////////////////////////////////
public static void main(String[] args) {
int numThreads = new Integer(args[0]).parseInt(args[0]);
System.out.println(numThreads);
for(int j=0; j<numThreads; j++){
ThreadedEngineTester trct = new ThreadedEngineTester();
trct.start();
}//for main thread loop
}
}

///////////////////////////////////////
and goTest(), inside run() adds a try/catch in the beginning where the Engine is loaded(),
//////////////////////////////////////

try{
long start = System.currentTimeMillis();
Engine.load();
long end = System.currentTimeMillis();

////////////////
with the associated catch block coming at the end of the method.

As you can see, the only real difference seems to be how many times Engine is load()ed.

Thanks for your help though.

alex


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top