Applet 生命周期:init() 和 init() 之间的实际区别是什么?start() 和 destroy() &停止()? [英] Applet lifecycle: what's the practical difference between init() & start(), and destroy() & stop()?

查看:35
本文介绍了Applet 生命周期:init() 和 init() 之间的实际区别是什么?start() 和 destroy() &停止()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javadoctutorial 有关于四种小程序生命周期方法的信息(init()-> start() -> stop() -> destroy()).但他们大多用抽象的语言交谈.

The javadoc and tutorial have information about the four applet lifecycle methods (init() -> start() -> stop() -> destroy()). But they talk mostly in abstract language.

我正在寻找的是具体示例,说明如果我将代码放在 initstart 中会产生什么不同,对于 destroy vs stop.到目前为止,我发现的唯一内容是教程对 destroy 方法的描述.它说:

What I'm looking for are concrete examples of when it makes a difference if I put my code in init vs start, and similarly for destroy vs stop. The only thing I've found so far is in the tutorial's description of the destroy method. It says:

注意:保持实现尽可能短的销毁方法,因为不能保证这种方法将完全执行.Java 虚拟机可能会在长时间销毁之前退出方法已完成.

Note: Keep implementations of the destroy method as short as possible, because there is no guarantee that this method will be completely executed. The Java Virtual Machine might exit before a long destroy method has completed.

(我有点震惊,上面的内容不在 javadoc 中.)

(I'm a bit shocked that the above isn't in the javadoc.)

更具体地说:任何人都可以提供浏览器 + JVM 组合,在某些特定操作(切换标签、点击后退"按钮等)时调用 停止但不是destroy(或start但不是init)?

to be more specific: Can anyone provide a browser + JVM combo that, upon some specific action (switching tabs, hitting the 'back' button, etc.), invokes stop but not destroy (or start but not init)?

推荐答案

initdestroy 分别在小程序加载或卸载时调用.浏览器可以在导航、切换标签等时加载小程序并停止它,但不会破坏它.

init and destroy are called when the applet is loaded or unloaded, respectively. It's possible for a browser to load an applet and stop it, but not destroy it, when navigating around, switching tabs, etc.

startstop 用于暂停和恢复小程序,在上述情况下(当小程序成为或不再显示在页面上时).

start and stop are for pausing and resuming the applet, in the case above (when the applet becomes, or ceases to be, shown on a page).

我不知道是否有任何浏览器确实让小程序保持加载状态,所以这可能无关紧要.但据我所知,一般规则是:

I don't know if any browser actually does keep an applet loaded, so it may not matter much. But as far as i learned it, the general rule is:

  • init 应该让小程序准备好运行,但实际上并未启动它.当从 init 返回时,applet 应该处于停止"状态.(停止的小程序应该使用尽可能少的资源,并且不使用 CPU.)
  • start 应该启动小程序运行(启动线程等).它通常不会读取参数和重新加载图像等等,因为这应该在 init 中完成.
  • stop 应该撤消 start 所做的事情......将小程序返回到停止"状态,但让它能够再次start.它不应该撤消任何 init 的工作,因为如果功能被正确分离,这将使小程序无法启动.
  • destroy 应该在卸载小程序之前进行任何最终清理.它基本上撤消了 init.它不应该停止小程序;这是 stop 的工作,应该在调用 destroy 之前已经完成.
  • init should get the applet ready to run, but not actually set it in motion. The applet should be in a "stopped" state upon return from init. (A stopped applet should be using as few resources as practically possible, and no CPU.)
  • start should start the applet running (starting threads, etc). It generally won't read params and reload images and all that, as that should be done in init.
  • stop should undo what start does...returning the applet to the "stopped" state, but leaving it able to start again. It should not undo any of init's work, as that would leave the applet unstartable if the functionality is properly separated.
  • destroy should do any final cleanup before the applet is unloaded. It basically undoes init. It should not stop the applet; that's stop's job, and should already be done before destroy is called.

这篇关于Applet 生命周期:init() 和 init() 之间的实际区别是什么?start() 和 destroy() &停止()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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