我在命令行应用程序中有以下代码。循环完成后,我的应用程序仍在运行。为什么不关闭。从日志中,我可以看到endIndex已达到。但该应用程序仍在运行?
Executor exec = Executors.newFixedThreadPool(4);
for (int i = startIndex; i <= endIndex; i++) {
final String spURL = urlPart + i;
Runnable requestHandler = new Runnable() {
@Override
public void run() {
try {
getImageForURL(spURL, 0);
} catch (IOException ex) {
} catch (Exception ex) {
}
}
};
exec.execute(requestHandler);
}
答案 0 :(得分:2)
试试这个
ExecutorService e = Executors.newFixedThreadPool(4);
...
e.shutdown();
答案 1 :(得分:0)
您需要在循环后关闭执行程序(理想情况下在finally
块中):
exec.shutdown()
或exec.shutdownNow()