604327650
V2EX  ›  问与答

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

  •  
  •   604327650 · Oct 17, 2017 · 1763 views
    This topic created in 3150 days ago, the information mentioned may be changed or developed.

    请教各位一个问题,在使用多线程处理集合时,需要设定一个 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)));
    
        }
    
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   974 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 21:44 · PVG 05:44 · LAX 14:44 · JFK 17:44
    ♥ Do have faith in what you're doing.