How to preload classes in Java?

妖精的绣舞 提交于 2019-12-07 20:52:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!