views:

238

answers:

2

Is it possible to implement a multi-threaded class loader in Java? In a meta-driven framework I need to load several hundreds of classes in advance, ie, not as lazily as the system classloader. In order to accelerate this, I would like to better utilize current multi-core CPUs. Before I dive into that, I would be interested if somebody already has some experience on this issue or if it is possibly totally clear that perhaps defineClass() is the bottleneck in this case.

Thanks Andre

A: 

There is nothing wrong with having one or more thread loading a class in the background which requires all the classes you need to preload. Do a prototype with an Executor and Callables so you can get some profiling information with jvisualvm.

Thorbjørn Ravn Andersen
Yes technically it should be possible to have multiple threads loading classes, that is quite clear. The defineClass() method is not synchronized, which is kind of a prerequisite. But I wonder if there is a synchronization point somewhere deeper in the JVM code which prevents real MT classloading without beeing obvious?
Andre Pareis
defineClass is not synchronized as class loading is defined as single threaded by the JVM. (In Java 5 and 6 at least) A few hundred classes don't take that long to load what is the time frame you need it to load in?
Peter Lawrey
I am basically only afraid that I base my framework on an architecture that does not scale very well. 100s of classes is just the beginning but as the framework gets more and more features the number of classes might go up to the thousands, which may lead to a serious latency at startup.
Andre Pareis
Well, if you need to load the classes, you need to load them. Have you done timings to indicate how long time it actually takes?
Thorbjørn Ravn Andersen
Yes you are right but if you don't split the loads across several pieces of let's say user interactions then you get a bad responsiveness in the beginning (and as a payoff a faster response time subsequently). It all depends on the kind of application that is developed. That said, I'd like to have both: no loads after initialization and a faster initialization. The fast initialization could be achieved by utilizing multiple cores for class loading, and that was exactly what I had in mind with my initial question ;)
Andre Pareis
Do it in a background thread...
Thorbjørn Ravn Andersen
+4  A: 

I believe currently you will hit an exclusive lock. In JDK7, class loaders will be able to mark themselves as being parallel-capable.

As ever, I suggest possibly doing some back-of-envelope calculations and then suck-it-and-see.

Tom Hawtin - tackline
OK, I'm going to check out the JDK 7.0 classloader API first. If you are correct with the lock in any JDK < 7.0 then there might be at least the benefit of parallel loading of byte code from the file system and preprocessing it. I will do some testing to see if this is good enough for my purposes.
Andre Pareis
Here's a link: http://openjdk.java.net/groups/core-libs/ClassLoaderProposal.html . I don't know of anything more up to date than that off hand.
Tom Hawtin - tackline
Thanks Tom, very comprehensive link showing the direction and the current situation.
Andre Pareis