Java Reference
In-Depth Information
There is an interesting trade-off here: whether to deploy the more complex code and slightly
hurt performance in the single-threaded case, or whether to optimize for other
cases—particularly when, as in the last example, the performance difference is quite large.
These are the sort of performance trade-offs that occur all the time, and in this case the JDK
team opted for the second choice as a default. As a platform, it is a good idea to provide both
choices (even though only one can be a default). Hence, the JDK 6 behavior can be enabled
by using the -XX:+AlwaysLockClassLoader flag (which is false by default). Long startup
cycles without concurrent threads loading classes from different classloaders may benefit
slightly from including that flag.
1. In complex applications (particularly application servers) with multiple classload-
ers, making those classloaders parallel-capable can solve issues where they are
bottlenecked on the system or bootclass classloader.
2. Applications that do a lot of classloading through a single classloader in a single
thread may benefit from disabling the parallel-capable feature of Java 7.
Random Numbers
Java 7 comes with three standard random number generator classes: java.util.Random ,
java.util.concurrent.ThreadLocalRandom , and . There
are important performance differences among these three classes.
The difference between the Random and ThreadLocalRandom classes is that the main opera-
tion (the nextGaussian() method) of the Random class is synchronized. That method is used
by any method that retrieves a random value, so that lock can become contended no matter
how the random number generator is used: if two threads use the same random number gen-
erator at the same time, one will have to wait for the other to complete its operation. This is
why the thread-local version is used: when each thread has its own random number generat-
or, the synchronization of the Random class is no longer an issue. (As discussed in Chapter 7 ,
the thread-local version also provides significant performance benefits because it is reusing
an expensive-to-create object.)
Search WWH ::

Custom Search