问题
The first opening of a new JInternalFrame in my Swing interface takes some time. According to the profiler, most of this time is spent in the Classloader.loadClass() method. I understand that it loads classes dynamically when needed and indeed the next time I open the same frame, it is much faster.
This application has a login screen, and then a time with an empty desktoppane before the user opens one of those internal frames. I figure that since I have some "down time", I could use it to preload some common classes in background, so that when the user actually opens an internal frame, it goes much faster.
Is there a way to achieve that, other than having to generate an entire path of "dummy contructors" through my panels and actually initialize an instance of those classes?
Ideally, if it could cascade such loading, it would be even better. A bit like the work done by classfileset in ant
回答1:
Assuming you're not doing anything special with classloaders, you can call Class.forName(String className)
for each class you want to pre-load.
This will execute static initializers in those classes, so it could affect the behaviour of your program (although I'd consider any such program to be badly-written).
来源:https://stackoverflow.com/questions/25178154/how-to-preload-classes-in-java