main方法里的代码走完了,程序不停止

本文探讨了在使用百度提供的Word转PDF工具代码时遇到的程序延迟问题,主要原因是未关闭的线程导致程序无法正常结束。通过debug分析和添加`converter.kill()`解决策略,作者分享了解决线程管理问题的方法。
摘要由CSDN通过智能技术生成

今天在main方法运行百度上复制的word转pdf的工具代码时发现main方法里的代码都走完了,但是程序就是不停止,   如下图

 

    public static void WordToPdf(String sourcePath, String targetPath) throws Exception {
        File inputWord = new File(sourcePath);
        File outputFile = new File(targetPath);
        InputStream docxInputStream = null;
        OutputStream outputStream = null;
        try {
            docxInputStream = new FileInputStream(inputWord);
            outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder()
                    .build();
            converter.convert(docxInputStream)
                    .as(DocumentType.DOCX)
                    .to(outputStream)
                    .as(DocumentType.PDF).execute();
//            converter.kill();
        } catch (Exception e) {
            throw new Exception("[documents4J] word转pdf失败");
        } finally {
            if (docxInputStream != null) {
                docxInputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }

这时候我用debug一步一步试,正常的debug的threads窗口应该是有M,A,F,R,S这5个,如下图

 但是我运行复制的word转pdf的工具代码到最后debug的线程显示多了一个.如图

 因为main方法自己的线程走完后,它不会去自动关闭其他线程,导致这个多的线程一直在等待运行,所以这个程序就一直不停止,所以最后我加了converter.kill(),把这个等待的线程杀死了,然后程序就能正常停止了.

补充一句:test测试注解是当程序走到最后一行大括号时,它会自动停止所有线程来终止程序.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值