免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5491 | 回复: 7
打印 上一主题 下一主题

java线程退出时资源清理问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-06-15 19:41 |只看该作者 |倒序浏览
10可用积分
我的代码利用Jacob来调用outlook, 但是发现一些问题:在调用Dispatch.call(oNameSpace, "Logoff"); 程序退出后,outlook 进程仍然运行在后台。

我有写了个简短的测试程序如下,发现可能是程序退出清理资源的问题。
如下这段代码是运行正确的,但加了System.exit(0) 这句话就会出现上述问题:outlook 进程仍然运行在后台。
单步运行调试发现,不加 System.exit(0);时, 程序最后会运行 Thread.exit()进行资源清理;但执行System.exit(0);的话, Thread.exit()就不会执行,所以我相信问题出现的原因就是Thread.exit()的清理工作没有运行。
但是我的程序里必须执行System.exit(0), 我的问题就是如何执行Thread.exit()的清理工作。

public static void main(String asArgs[]) throws Exception {
System.out.println("Outlook: IN");
//Dispatch oOutlook;

ActiveXComponent axOutlook = new ActiveXComponent("Outlook.Application");
try {

System.out.println("version=" + axOutlook.getProperty("Version"));

Dispatch oOutlook = axOutlook.getObject();
System.out.println("version=" + Dispatch.get(oOutlook, "Version"));

Dispatch oNameSpace = axOutlook.getProperty("Session").toDispatch();
Dispatch.call(oNameSpace, "Logon");
System.out.println("Logoff");
//System.exit(0);

} finally {
//axOutlook.invoke("Logoff", new Variant[] {});
//Dispatch.call(oOutlook, "Logoff");
}
}

下面是Thread.exit()代码,请问大家这个问题有什么解决办法, 或者有什么建议, 怎样做 Thread.exit()的工作呢?
   /**
     * This method is called by the system to give a Thread
     * a chance to clean up before it actually exits.
     */
    private void exit() {
        if (group != null) {
            group.remove(this);
            group = null;
        }
        /* Aggressively null out all reference fields: see bug 4006245 */
        target = null;
        /* Speed the release of some of these resources */
        threadLocals = null;
        inheritableThreadLocals = null;
        inheritedAccessControlContext = null;
        blocker = null;
        uncaughtExceptionHandler = null;
    }

[ 本帖最后由 robertsucn 于 2009-6-15 19:59 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-06-16 10:47 |只看该作者

回复 #1 robertsucn 的帖子

两个方法不能串行调用?

论坛徽章:
0
3 [报告]
发表于 2009-06-16 12:51 |只看该作者

回复 #2 ziggler 的帖子

Thread.exit()是private方法,没法调用

论坛徽章:
0
4 [报告]
发表于 2009-06-16 12:53 |只看该作者

回复 #3 robertsucn 的帖子

自己写一个,成为可调用的?

论坛徽章:
0
5 [报告]
发表于 2009-06-21 00:13 |只看该作者

回复 #1 robertsucn 的帖子

System.exit(0);是终止virtual machine的运行,这样自然,finally语句就得不到执行。
将System.exit(0)放在finally语句块之后,finally语句块里写thread.exit()代码会可以。

论坛徽章:
0
6 [报告]
发表于 2009-06-21 12:56 |只看该作者
原帖由 charlieli1 于 2009-6-21 00:13 发表
System.exit(0);是终止virtual machine的运行,这样自然,finally语句就得不到执行。
将System.exit(0)放在finally语句块之后,finally语句块里写thread.exit()代码会可以。




学习了

论坛徽章:
0
7 [报告]
发表于 2009-07-01 16:32 |只看该作者

回复 #5 charlieli1 的帖子

退出后不用清理吧

论坛徽章:
0
8 [报告]
发表于 2009-07-02 10:51 |只看该作者
在执行System.exit(0)之前把outlook的那个线程关掉
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP