I have List
of N
items and I want to divide this List
in a sequential manner between a fixed number of threads
.
By sequential I mean, I want to pass 1 to N/4
to first thread
, N/4 + 1 to N/2
to second thread and N/2+1 to N
to third thread
, Now once all the threads
have finished their work, I want to notify to main thread
to send some message that all the processing has been completed.
What I have done so far now is that I have implemented ExecutorService
I did something like this
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");
}
- How to divide list in sequential chunks?
- is there a better way to achieve such functionality ?