【发布时间】:2015-08-19 05:17:05
【问题描述】:
我有List 的N 项目,我想在固定数量的threads 之间按顺序划分这个List。
我的意思是,我想将1 to N/4传递给第一个thread,N/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");
}
- 如何将列表分成连续的块?
- 有没有更好的方法来实现这样的功能?
【问题讨论】:
-
查看java.util.concurrent中的Fork/Join框架。 docs.oracle.com/javase/tutorial/essential/concurrency/…中有很多例子
标签: java multithreading concurrency threadpoolexecutor