I have a big program in Java that uses multithreading. In some cases, the program starts using 100% of three cores of my eight core system. In normal use, the program use all cores at 1-2%. How can I find the class that's overloading cores?
Try taking threads dump (see jps, jstack commands) and then see which methods are executed.
Use a profiler such as the jvisualvm that is bundled with jdk-1.6.0_10
Use a profiler to figure out which thread(s) are using all of your CPU cycles, and the method(s) they are executing.
If you are using Eclipse, you can use the TPTP profiling tool.
JProbe is a popular commercial profiler. You can download it and try it out for free. As crowne suggested, the profiling utilities packaged with Java 1.6 are pretty good as well.
If you are going the commercial profiler route then I would recommend using Dynatrace.
If you are using Java over UNIX or some versions of Linux look into DTrace with Java.
The best solution is to use a profiler - that's what they're built for, and there's a great one bundled with Java 6.
Another (far from being as ideal a solution) is to run your program in the Eclipse IDE (if that's what you use) in debug mode. You can then look at the running threads. IF a lot of them are suspended, the one that is not might be your culprit. Force it to break (from the toolbar) and you can see where it is. There are many chances that you'll find a clear loop or busy waiting.
Sampling is an dated, flawed and ill-suited solution for todays enterprise Java applications with hundreds of threads and deep call stacks. It has limited success in desktop applications - single threads and simplistic.
Its best to use an execution based profiler because for those with the thread dump stack blinkers on we are talking about CPU time not wall clock time and this is an intermittent issue.
Some further reading: http://williamlouth.wordpress.com/2009/01/16/profiling-sampling-versus-execution-part-2/ http://williamlouth.wordpress.com/2009/10/21/java-call-sampling-overhead-in-the-wild/
With an execution and strategy based metering solution this is pretty easy. Check out our Busy and BusyThread strategy docs on our OpenCore community website. These enable metering (measurement, profiling) automatically when such situations are detected.