为什么多个小程序可以访问一个全局类? [英] Why can multiple applets access one global class?

查看:19
本文介绍了为什么多个小程序可以访问一个全局类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几年前我有一个相当复杂的小程序结构,我有一个关于其中的全局类的问题.基本上,我在同一页面上有三个小程序,它们以不同的方式相互交互.我感到困惑的部分是它们都共享来自同一类的全局(静态)变量.这究竟是怎么发生的?我的印象是每个小程序都有自己的小环境,因此无法访问与其他小程序相同的静态变量?例如,如果我在小程序 A 中有这个:

So I have a fairly complex applet structure from a couple years back, and I have a question about a global class that is in it. Basically, I have three applets on the same page and they interact with each other in different ways. The part I'm confused about is the fact that they all share global (static) variables from the same class. How exactly is this happening? I was under the impression that each applet would have its own little environment and would thus be unable to access the same static variables as the others? For example if I have this in applet A:

Globals.globalVar = 5;

这在小程序 B 中:

int x = Globals.globalVar;

并且我确保 B 在 A 之后启动,然后 x 将变为 5.我想知道这在 JVM 中究竟是如何处理的,因为它对我来说没有任何意义.这种行为是否意味着如果您同时运行两个单独的 Java 应用程序,它们可以访问彼此的静态变量?先谢谢了.

And I ensure that B starts AFTER A then x will become 5. I would like to know how exactly this is handled in the JVM, as it doesn't quite make any sense to me. Does this behaviour imply that if you have two separate java applications running at the same time they can access each others static variables? Thanks beforehand.

推荐答案

因为它是这样定义的,出于向后兼容性的原因.请参见 http://download.oracle.com/javase/6/docs/technotes/guides/jweb/applet/applet_execution.html#cache:

Because that's how it's defined, for backward-compatibility reasons. See http://download.oracle.com/javase/6/docs/technotes/guides/jweb/applet/applet_execution.html#cache:

通常,如果两个小程序具有相同的代码库和存档参数,它们将由同一个类加载器实例加载.此行为是向后兼容所必需的,并且依赖于通过几个现实世界的应用程序.结果是多个同一个网页上的小程序可以访问彼此的静态变量在 Java 语言层面,有效地允许多个小程序将它们写成就好像它们包含一个应用程序一样.

Normally, if two applets have the same codebase and archive parameters, they will be loaded by the same class loader instance. This behavior is required for backward compatibility, and is relied on by several real-world applications. The result is that multiple applets on the same web page may access each others' static variables at the Java language level, effectively allowing the multiple applets to be written as though they comprised a single application.

虽然此功能使某些类型的应用程序能够写起来方便,但也有一定的缺点.它干扰小程序的终止,特别是当多个实例相同的小程序处于活动状态.它使小程序的编程模型更复杂,因为当静态小程序的字段将被重新初始化,以及何时将被重新初始化从运行到运行维护相同的小程序.导致不精确Java 插件中某些用户界面操作的行为由于无法准确识别哪个小程序启动了特殊要求.

While this feature enables certain kinds of applications to be conveniently written, it has certain drawbacks. It interferes with termination of applets, in particular when multiple instances of the same applet are active. It makes the programming model for applets more complex, since it is under specified exactly when the static fields of an applet will be re-initialized, and when they will be maintained from run to run of the same applet. It causes imprecise behavior of certain user interface operations within the Java Plug-in due to the inability to identify exactly which applet initiated a particular request.

因此,新的 Java 插件提供了一种选择退出在一个小程序的基础上使用类加载器缓存.

For this reason, the new Java Plug-in provides a way to opt out of the use of the classloader cache on an applet by applet basis.

这篇关于为什么多个小程序可以访问一个全局类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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