问题描述
javadoc 和 tutorial 有关于四种小程序生命周期方法的信息(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.
我正在寻找的是具体示例,说明如果我将代码放在 init
和 start
中会产生什么不同,对于 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 虚拟机可能会在长时间销毁之前退出方法已完成.
(我有点震惊,上面的内容不在 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
)?
推荐答案
init
和 destroy
分别在小程序加载或卸载时调用.浏览器可以在导航、切换标签等时加载小程序并停止它,但不会破坏它.
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.
start
和 stop
用于暂停和恢复小程序,在上述情况下(当小程序成为或不再显示在页面上时).
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 frominit
. (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 ininit
.stop
should undo whatstart
does...returning the applet to the "stopped" state, but leaving it able tostart
again. It should not undo any ofinit
'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 undoesinit
. It should not stop the applet; that'sstop
's job, and should already be done beforedestroy
is called.
这篇关于Applet 生命周期:init() 和 init() 之间的实际区别是什么?start() 和 destroy() &停止()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!