V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
604327650
V2EX  ›  问与答

[请教] Executors.newFixedThreadPool(nThreads); 是否会对全局造成影响?

  •  
  •   604327650 · 2017-10-17 22:38:28 +08:00 · 1469 次点击
    这是一个创建于 2379 天前的主题,其中的信息可能已经有所发展或是发生改变。

    请教各位一个问题,在使用多线程处理集合时,需要设定一个 nThreads,请问如下方式是否会与全局( xml 文件)设定的线程池冲突?

        int dealSize = 200000; // 每次处理的数量
        int index = 0; // 每组的起点下标
        int nThreads = 5; // 线程数量
        ExecutorService service = Executors.newFixedThreadPool(nThreads);
        List<Future<List<String>>> futures = new LinkedList<Future<List<String>>>();
    
        for (int i = 0; i < nThreads; i++)
        {
            int start = index;
            index += dealSize;
            if (start >= list.size())
                break;
            int end = start + dealSize;
            end = end > list.size() ? list.size() : end;
            futures.add(service.submit(new Task(list, start, end)));
    
        }
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3300 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:19 · PVG 20:19 · LAX 05:19 · JFK 08:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.