I have a contact pointer to the bottleneck in my application, it seems to me that it comes down to calling Thread :: setContextClassLoader .
Basically, I was forced to mess with the thread context class loader due to problems with third-party libraries (see this question to see why).
The solution I chose was, as far as I know, general, and it works something like this:
Thread thread = Thread.currentThread();
ClassLoader old = thread.getContextClassLoader();
thread.setContextClassLoader(newClassLoader);
try {
... // problematic code that uses the thread context class loader
} finally {
thread.setContextClassLoader(old);
}
It turned out that calling setContextClassLoader was not a problem when only one thread was running, but when more than one thread was running, it slows down dramatically.
I made the following test application to isolate the problem:
ArrayList<Thread> threads = new ArrayList<Thread>();
int thread_count = 1;
long start = System.currentTimeMillis();
for (int i = 0; i < thread_count; i++) {
Thread thread = new Thread(new MyRunnable(100000000));
thread.start();
threads.add(thread);
}
for (Thread thread : threads) {
thread.join();
}
long total = System.currentTimeMillis() - start;
double seconds = (double)total / 1000;
System.out.println("time in seconds: " + seconds);
And this is the MyRunnable class:
public class MyRunnable implements Runnable {
int _iterations;
public MyRunnable(int iterations) {
_iterations = iterations;
}
public void run() {
final Thread curr = Thread.currentThread();
final ClassLoader loader = ClassLoader.getSystemClassLoader();
for (int i = 0; i < _iterations; i++) {
curr.setContextClassLoader(loader);
}
}
}
.
. thread_count 1, . 2 1.5 ~, 3 2.7 ~, 4 4 ~ - .
Thread setContextClassLoader, , , , - - , . ( , ), .
?
P.S. JRE 1.5, 1.6.
EDIT: @Tom Hawtin - . , , , . , , > 1.