qq_sxaqsqw 2021-09-07 08:51 采纳率: 100%
浏览 54
已结题

java 多线程任务停不下来


class loops//不知道为什么,停不下来
{
    static class LOOP implements Callable<Integer>
    {
        int c;
        LOOP(int c)
        {
            this.c=c;
        }

        int n=0;
        public Integer  call()
        {
            for(int i=0;i<100000;i++)
                n=i;
            return n;
        }
    }
    public static void main(String[] args) throws Exception
    {
        ExecutorService exec=Executors.newCachedThreadPool();
        List<Future<Integer>> fc=new ArrayList<Future<Integer>>();
        for(int i=0;i<10;i++)
        {
            fc.add(exec.submit(new LOOP(i)));
        }
        for(Future<Integer > gc:fc)
        {
            System.out.println(gc.get());
        }

    }

}

运行程序后,停不下来

  • 写回答

5条回答 默认 最新

  • 曦陽惜夏 2021-09-07 09:56
    关注

    exec 线程池是需要关闭的,如果不关闭,即使添加的线程都执行完,且会有一个线程一直等待状态,自然就不会关闭,需要手动关闭exec.shutdown();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 9月15日
  • 已采纳回答 9月7日
  • 创建了问题 9月7日

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记