为什么这个小程序有一个 destroy 方法?
我一直在尝试通过研究 “欢迎使用 HotJava” 小程序。 我使用 Windows 版本的 Jad 反编译了 *.class 文件,我看到以下几行代码
public void init() {
// Skip some lines...
addMouseListener(this);
}
public void destroy()
{
removeMouseListener(this);
}
这里的 destroy 方法真的有必要吗? 如果小程序即将结束,为什么需要将其自身作为鼠标侦听器删除?
I've been trying to learn about applets by studying the code for the "Welcome to HotJava" applet. I decompiled the *.class file using the Windows version of Jad, and I see the following lines of code
public void init() {
// Skip some lines...
addMouseListener(this);
}
public void destroy()
{
removeMouseListener(this);
}
Is the destroy method really necessary here? Why does the applet need to remove itself as a mouse listener if it's just about to end?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
destroy()
方法清理资源以便释放它们。 当整个 JVM 将关闭时,在关闭之前释放所有资源并不那么重要,但即使不是绝对必要,执行正确的操作始终是一个好主意。根据线程模型,如果您将自己保留为鼠标侦听器,那么如果发生鼠标事件,您仍然会收到通知。 如果同一个 JVM 中有多个 Applet,并且只有一个 Applet 正在结束,那么如果您留下一个不会对其执行任何操作的侦听器,则可以使线程处于一种有趣的状态。 这样做可能会锁定其他小程序。
编辑:
通过处于有趣状态的线程,我的意思是(例如)如果 Applet 线程已停止的侦听器正在将消息排队到无人读取的队列,那么最终队列将填满并且调度线程将阻塞。 ,我们假设侦听器除了对消息进行排队之外什么都不做,并且 Applet 中有一个线程(现已停止)从该队列中读取数据。一旦队列填满,它将阻塞!)
(更详细地说 具体来说,假设 Applet 退出时它不再可见并且无法再接收鼠标事件,那么您可能很安全。 然而,如果有不同类型的听众,你可能会遇到麻烦。 始终做正确的事情,即使并非真正必要,也会让您养成习惯,这样您就不会忘记在真正关键的时候做正确的事情。 :)
The
destroy()
method cleans up resources so they can be released. When the whole JVM will be shutting down, it's not as critical to release all resources before shutting down, but it's always a good idea to do The Right Thing even when it is not strictly necessary.Depending on the threading model, if you leave yourself as a mouse listener, then you will still be notified if mouse events occur. If there are multiple Applets in the same JVM and only one Applet is ending, then you can leave threads in a funny state if you leave a listener to which no action will be taken. It's possible that you can lock up the other Applets by doing so.
EDIT:
By threads in a funny state, I mean (for example) if a listener whose Applet thread has stopped is queuing messages to a queue that no-one is reading from, then eventually the queue will fill up and the dispatch thread will block. (In more detail, let's assume the listener does nothing but queue the message and that there is a thread in the Applet -- now stopped -- that reads from this queue. Once the queue fills, it will block!)
With a mouse listener, specifically, you may well be safe, assuming when the Applet quits it is no longer visible and can no longer receive mouse events. With a different sort of listener, however, you could get into trouble. Always doing the right thing, even when it is not truly necessary, gets you in the habit so you don't forget to do the right thing when it's actually critical. :)
如果您想留下您的小程序曾经运行过的任何“证据”,那么 destroy() 至关重要...
例如,您可以将所有状态信息发送到文件或服务器以供后续使用,或者让服务器知道你正在断开连接。
想象一下您有一个聊天应用程序......
The destroy() is critical if you want to leave any "evidence" that your applet had ever run...
For example, you could send all your state information to a file or a server for subsequent use, or let the server know that you are disconnecting.
Imagine that you had a chat application...
它对于释放小程序上下文之外存在的资源很有用。 假设您已经从外部服务器端应用程序获取了资源,例如许可证。 或者,您可能需要通知某些服务器端资源,应用程序已因统计或其他原因而关闭。
Its useful in releasing resources that exist outside the context of the applet. Lets say you have acquired a resource from a foreign server side application, like a license. Or perhaps you need to notify some server side resource that the application has shutdown for statistics or some other reason.
嗯,在这个特定情况下并非如此。 然而,在 java 中删除鼠标(和其他)侦听器是一个很好的做法 - 不这样做可能会导致不幸的内存泄漏。
对于
destroy()
来说,清理init()
所做的一切也是一个很好的做法,即使它是不必要的。Well, not in this particular case. However, it's good practise to remove mouse (and other) listeners in java - not doing so can result in unfortunate memory leaks.
And it's also good practise for your
destroy()
to clean up everything thatinit()
does, even when it's unnecessary.