今天在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测试注解是当程序走到最后一行大括号时,它会自动停止所有线程来终止程序.