繁体   English   中英

具有多个线程的'N'项的进程列表

[英]Process list of 'N' items with multiple threads

我有N项目的List ,我想在固定数量的threads之间按顺序划分此List

顺序我的意思是,我想传递1 to N/4到第一个threadN/4 + 1 to N/2到第二个线程和N/2+1 to N到第三个thread ,现在一旦所有threads完成了他们的工作,我想通知主thread发送一些消息,说明所有处理都已完成。

到目前为止我所做的是我已经实现了ExecutorService

我做了这样的事

ExecutorService threadPool = Executors.newFixedThreadPool(Number_of_threads); 
                //List of items List
                List <items>itemList = getList(); 
                  for (int i = 0 i < Number_of_threads ;i++ ) { 
                    //how to divide list here sequentially and pass it to some processor while will process those items.
                    Runnable processor = new Processor(Start, End)
                    executor.execute(process);
                   }
                  if(executor.isTerminated()){
                    logger.info("All threads completed");
                  }
  • 如何在顺序块中划分列表?
  • 有没有更好的方法来实现这样的功能?

如果您想要的是让所有线程尽快完成处理并且项目数量不是很大,那么只需将每个项目的一个Runnable发布到newFixedThreadPool(NUMBER_OF_THREADS)

    ExecutorService exec = Executors.newFixedThreadPool(NUMBER_OF_THREADS);
    List<Future<?>> futures = new ArrayList<Future<?>>(NUMBER_OF_ITEMS);
    for (Item item : getItems()) {
        futures.add(exec.submit(new Processor(item)));
    }
    for (Future<?> f : futures) {
        f.get(); // wait for a processor to complete
    }
    logger.info("all items processed");

如果你真的想给每个线程一个连续的列表部分(但仍希望它们尽可能快地完成,并且还希望处理每个项目花费大约相同的时间),那么将项目拆分为“均匀”你可以这样,每个线程的最大项目数不同于最小数量不超过一个(例如: 14项, 4线程,那么你希望分裂为[4,4,3,3] ,而不是例如[3,3,3,5] )。 为此,您的代码将是例如

    ExecutorService exec = Executors.newFixedThreadPool(NUMBER_OF_THREADS);
    List<Item> items = getItems();
    int minItemsPerThread = NUMBER_OF_ITEMS / NUMBER_OF_THREADS;
    int maxItemsPerThread = minItemsPerThread + 1;
    int threadsWithMaxItems = NUMBER_OF_ITEMS - NUMBER_OF_THREADS * minItemsPerThread;
    int start = 0;
    List<Future<?>> futures = new ArrayList<Future<?>>(NUMBER_OF_ITEMS);
    for (int i = 0; i < NUMBER_OF_THREADS; i++) {
        int itemsCount = (i < threadsWithMaxItems ? maxItemsPerThread : minItemsPerThread);
        int end = start + itemsCount;
        Runnable r = new Processor(items.subList(start, end));
        futures.add(exec.submit(r));
        start = end;
    }
    for (Future<?> f : futures) {
        f.get();
    }
    logger.info("all items processed");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM