Java Reference
In-Depth Information
Prior to Java 7, the problem here was that the method to load the class was synchronized:
only one classloader could delegate to the system classloader at a time. This drastically re-
duced any parallelization from using multiple classloaders, since each had to wait its turn for
access into the system and bootstrap classloaders. Java 7 addresses that situation by using a
set of locks based on the class name. Now, two classloaders that are looking for the same
class will still contend for a lock, but classloaders that are looking for different classes can
execute within the class hierarchy in parallel.
This benefit also applies if Java-supplied classloaders such as the URLClassLoader are used.
In Java 6, a URL classloader that was the parent of additional classloaders would be a syn-
chronization bottleneck for parallel operations; in Java 7, the classloader is able to be used in
parallel. Java 7-supplied classloaders are termed parallel-capable .
Classloaders are not parallel-capable by default. If you want to extend the benefits of parallel
use into your own classloaders, you must take steps to make them parallel-capable. There are
two steps.
First, ensure that the classloader hierarchy does not contain any cycles. A cyclical classloader
hierarchy is fairly uncommon. It also results in code that is quite difficult to maintain, since
at some point in the cycle, some classloader must directly satisfy the request rather than
passing the request to its parent classloader (otherwise, the delegation would have an infinite
loop). So while it is technically possible to enable parallelism for a cyclical series of class-
loaders, it is a convoluted process (on top of already convoluted code). Since one precept of
performant Java code is to employ common idioms and write simple code that the compiler
can easily optimize, that case will not be recommended here.
Second, register the classloader as parallel-capable in a static initializer within the class:
public
public class
class MyCustomClassLoader
MyCustomClassLoader extends
extends SecureClassLoader {
static
static {
registerAsParallelCapable ();
}
....
}
This call must be made within each specific classloader implementation. The Se-
cureClassLoader is itself parallel-capable, but subclasses do not pick up that setting. If,
Search WWH ::




Custom Search