相关疑难解决方法(0)

Java:强制停止ExecutorService线程

我的代码:


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()仅在循环或等待中工作的方法.如果这一刻发生,超时不起作用.那么,怎么样?

java multithreading executorservice

5
推荐指数
3
解决办法
3万
查看次数

标签 统计

executorservice ×1

java ×1

multithreading ×1