我的代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
但有时洪流下载需要未知的时间价值,« awaitTermination
»不能按我的意愿工作.我需要在半小时后立即停止所有执行的线程,但据我所知« awaitTermination
»只使用interrupt()
仅在循环或等待中工作的方法.如果这一刻发生,超时不起作用.那么,怎么样?