如何在Java中预加载类? [英] How to preload classes in Java?

查看:211
本文介绍了如何在Java中预加载类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Swing界面中第一次打开新的JInternalFrame需要一些时间。根据分析器,大部分时间都花在Classloader.loadClass()方法中。我知道它会在需要时动态加载类,实际上下次打开相同的帧时,它会快得多。

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?

理想情况下,如果它可以级联这样的加载,它会更好。有点像ant中的classfileset所做的工作

推荐答案

假设你没有对类加载器做任何特别的事情,您可以为要预加载的每个类调用 Class.forName(String className)

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).

这篇关于如何在Java中预加载类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆