Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concurrently process elements in a Collection in Java

I need to process elements in some Collection instance concurrently. In other words instead of iterating a Collection instance

for (Someclass elem : coll){
     process(elem);
}

I’d like to process those elements concurrently. Say, something like ConcurrentCollectionExecutor(coll, new Callable{…}, numberOfThreads). Also, a number of simultaneous threads should be fixed.

Any flexible pattern already exists?

like image 965
serg kunz Avatar asked Feb 08 '13 08:02

serg kunz


2 Answers

Make the process method a run() method in a class called MyRunnable that implements Runnable and whose constructor takes elem as input and stores it as an instance variable. Then use:

ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads);
for (Someclass elem : coll){
   Runnable worker = new MyRunnable(elem);
   executor.execute(worker);
}
like image 115
stepanian Avatar answered Nov 05 '22 06:11

stepanian


A good solution would be:

  1. instantiate an ArrayBlockingQueue containing the elements to process
  2. instantiate an ExecutorService to execute your processing concurrently
  3. instantiate your Runnables giving them the ArrayBlockingQueue as parameter
  4. Implement the run method: while there elements in the queue, poll them and process them
  5. Submit your Runnables to the ExecutorService

The code:

BlockingQueue<Someclass> toProcess = 
    new ArrayBlockingQueue<Someclass>(coll.size(), false, coll);
ExecutorService es = Executors.newFixedThreadPool(numberOfThreads);
for(int count = 0 ; count < numberOfThreads ; ++c) {
    es.submit(new MyRunnable(toProcess));
}


private static class MyRunnable() implements Runnable {
    private final BlockingQueue<Someclass> toProcess;

    public MyRunnable(BlockingQueue<Someclass> toProcess) {
        this.toProcess = toProcess;
    }

    @Override
    public void run() {
        Someclass element = null;
        while((element = toProcess.poll()) != null) {
            process(element);
        }
    }
}
like image 35
Jean Logeart Avatar answered Nov 05 '22 06:11

Jean Logeart