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

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

问题描述

所以我有从几年一个相当复杂的小程序结构回来,我有一个关于一个全球性的类,它是它的问题。基本上,我有同一个页面的三个小程序,它们相互以不同的方式进行交互。我感到困惑的部分是,他们都共享同一类全局(静态)变量的事实。究竟是如何会出现这种情况?我是IM pression下,每个小程序将有其自己的小环境,从而将无法访问同一个静态变量的其他人呢?例如,如果我有这个小程序中的一个:

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;

和这小应用程序A:

int x = Globals.globalVar;

和我保证B开头的,然后进行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.

推荐答案

由于这是它是如何定义的,向后兼容的原因。见<一href=\"http://download.oracle.com/javase/6/docs/technotes/guides/jweb/applet/applet_execution.html#cache\" rel=\"nofollow\">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:

通常情况下,如果两个小程序具有相同的codeBase类和归档
  参数,它们将用相同的类加载器实例被加载。
  需要向后兼容这种行为,并依靠
  几个现实世界的应用。其结果是,多
  在同一网页上的小程序可以访问对方的静态变量
  在Java语言水平,有效地使多个applet
  就好像它们由一个单一的应用程序编写。

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.

虽然这个功能允许某些类型的应用程序是
  便利地写入,它具有一定的缺点。它干扰
  小应用程序的终止,特别是当的多个实例
  同一个applet是活动的。它使小应用程序编程模型
  更复杂的,因为它是根据指定恰好当静
  一个小程序的字段将被重新初始化,并且当他们将
  从运行保持运行相同的小程序。它会导致IM precise
  在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.

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

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