java – 首先使用main方法加载类,然后手动加载其他类
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 首先使用main方法加载类,然后手动加载其他类,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1653字,纯文字阅读大概需要3分钟。
内容图文
![java – 首先使用main方法加载类,然后手动加载其他类](/upload/InfoBanner/zyjiaocheng/786/602079061bc6423e9bb3ba8bb6d437ef.jpg)
我正在阅读Cay Horstmann撰写的Core Java.
这里有一段我??无法理解的摘录:
At startup, the class containing your mainmethod is loaded. It loads
all classes that it needs. Each of those loaded classes loads the
classes that it needs, and so on. That can take a long time for a big
application, frustrating the user. You can give users of your program the illusion of a faster
start with the following trick. Make sure that the class containing themainmethod does not
explicitly refer to other classes. First display a splash screen. Then manually force the loading
of other classes by calling Class.forName.
你能给我一个代码的小例子,这样我才能理解它吗?
解决方法:
引用中提到的技巧基本上是一种方式,通过在调用程序后“立即”显示启动画面,然后开始加载类,给用户一种幻觉,即应用程序加载速度更快.
假设您的应用程序的主类是TheActualApplication.
因此,如果您将FastSplash作为应用程序的启动类,则可以比将SlowSplash用作主类更快地显示启动屏幕.
public class FastSplasher {
public static void main(String[] args) {
SplashWindow.splash(Splasher.class.getResource("splash.gif"));
SplashWindow.invokeMain("TheActualApplication", args);
SplashWindow.disposeSplash();
}
}
public class SlowSplasher {
public static void main(String[] args) {
SplashWindow.splash(Splasher.class.getResource("splash.gif"));
TheActualApplication.main(args);
SplashWindow.disposeSplash();
}
}
使用FastSplash时可能更快发生启动画面的原因是因为今天的Java VM通常打开了lazy class resolution,因此您可能看不到很大差异.在急切的类解析的情况下,只有在加载了TheActualApplication中使用的所有类之后,才会显示SlowSplasher中的启动.在FastSplasher中,启动屏幕立即出现,因为类TheActualApplication在运行时使用反射动态加载,无法静态解析,因此在启动时加载.
内容总结
以上是互联网集市为您收集整理的java – 首先使用main方法加载类,然后手动加载其他类全部内容,希望文章能够帮你解决java – 首先使用main方法加载类,然后手动加载其他类所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。