zhyiwww
用平实的笔,记录编程路上的点点滴滴………
posts - 536,comments - 394,trackbacks - 0

看如下代码:

    public void destroy() {
        super.destroy();
        if (runner != null) {
            runner.stop();
            runner = null;
        }
        if (thread != null) {
            thread.interrupt();
            thread = null;
        }
    }

这是摘录的一段代码。应该说对资源释放时的问题做的比较到位。

  if (runner != null)语句可以有效的避免NullPointException异常的抛出

     runner.stop();可以释放运行阶段的资源。

          runner = null; 把runner对象置空,有利于资源的快速回收。


综上,此段代码既有稳健性考虑,又有适当性能提升考虑,在我们写代码时值得借鉴。







|----------------------------------------------------------------------------------------|
                           版权声明  版权所有 @zhyiwww
            引用请注明来源 http://www.blogjava.net/zhyiwww   
|----------------------------------------------------------------------------------------|
posted on 2011-08-11 11:25 zhyiwww 阅读(387) 评论(0)  编辑  收藏 所属分类: j2ee

只有注册用户登录后才能发表评论。


网站导航: