引自Java Tutorials:
The Java Plug-in software creates a worker thread for every Java
applet .
在多线程环境中,您应该非常小心共享资源.最好和最简单的方法是不分享任何东西(最好的扩展,没有死锁).
我假设,每次在“init”-method中初始化你的处理程序.如果是的话,您应该使用一个静态共享记录器(请参阅此link).它将有助于改善情况,但如果您使用applet启动多个浏览器 – 仍将创建新的日志文件. Oracle不建议使用此解决方法,并保留这种解决方法以实现向后兼容性.
推荐且易于实现的解决方案 – “每个applet都应该拥有自己的记录器并写入自己的文件”.日志文件名生成代码:
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
private String generateFileName() {
return String.format("applets-log/%s-%s.log",dateFormat.format(new Date()),UUID.randomUUID());
}
回答您的小问题(已更改):
根据java插件中对此old bug的讨论,applet可以随时以一些预定义的间隔终止进行清理.因此,您应该将资源清理代码放在“停止”或“销毁”方法中,但您不应该依赖该代码的执行.
Applets lifecycle is controlled by browser and applets should not be
given capabilities to run when its hosting document is destroyed by
browser.
Since 6u10,both old plugin and new plugin enforce applet shutdown
after a fixed amount of time (1000ms in old plugin and 200ms in new
plugin) for applet to stop.