Windows, threads with a higher Java priority tend to run more than threads with a lower pri-
ority, but even low-priority threads get a fair amount of CPU time.
In either case, you cannot depend on the priority of a thread to affect its performance. If
some tasks are more important than other tasks, application logic must be used to prioritize
One way this can be done (to some extent) is to assign the tasks to different thread pools and
Monitoring Threads and Locks
When analyzing an application's performance for the efficiency of threading and synchroniz-
ation, there are two things to look for: the overall number of threads (to make sure it is
neither too high nor too low), and the amount of time threads spend waiting for a lock or oth-
Virtually every JVM monitoring tool provides information about the number of threads (and
what they are doing). Interactive tools like jconsole show the state of threads within the
JVM. On the jconsole Threads panel, you can watch in real time as the number of threads
increases and decreases during the execution of your program. Figure 9-2 shows an example.
At one point in time, the application (NetBeans) was using a maximum of 45 threads. There
was a burst at the beginning of the graph where it was using up to 38, but it settled on using
between 30 and 31. jconsole can also print an individual thread stack; as the figure shows,
the Java2D Disposer thread is presently waiting on a reference queue lock.