Although creating a thread is a simple operation in Java, very often thread creation, management, and scheduling can be costly operations from the perspective of the Java virtual machine and the operating system, regardless of the thread model used.
Too many threads all using a large amount of CPU time while competing
for a small number of CPUs will not improve
performance, but will instead create substantial execution overhead.
A large number of threads (100 or more) can be justified only in cases
when the threads will be mostly waiting (java.lang.Object.wait)
for a condition to happen.
It might be worthwhile to experiment with a different number of threads,
to see how the performance is affected.
The threads should be as independent of each other as possible, which keeps the need for synchronization to a minimum. Incorrectly organized thread synchronization can also lead to inefficient use of threads.
Always use java.lang.Object.wait and
java.lang.Object.notify for thread synchronization.
Do not use notifyAll where notify
is sufficient.
HPjmeter displays a thread lifetime histogram which clearly indicates the creation and death times of each thread in the application. Additionally, most metrics can be viewed on a per-thread basis. This can help you to analyze which individual thread or group of threads is causing a performance bottleneck in your application.