我的代码:
String[] torrentFiles = new File("/root/torrents/").list();
if(torrentFiles.length == 0 || torrentFiles == null)
{
System.exit(0);
}
ex = Executors.newFixedThreadPool(3);
for(String torrentFile : torrentFiles)
{
ex.submit(new DownloadTorrent("/root/torrents/" + torrentFile));
}
ex.shutdown();
try
{
ex.awaitTermination(30, TimeUnit.MINUTES);
}
catch(InterruptedException ex1)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex1);
}
但有时 torrent 下载需要未知的时间价值,并且 « awaitTermination
» 不能按我的意愿工作。我需要在半小时后立即停止所有执行的线程,但据我所知« awaitTermination
» 只使用interrupt()
仅在循环或等待中有效的方法。因此,如果这一刻发生,超时将不起作用。那么,怎么做?