Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to process a list of objects in parallel processing in Java

I have a list of objects in Java like thousand objects in a List and I am iterating the List for every object and further processing . The same processing is hapening for every objects. This sequentail approach is taking much time for processing so, I want to achieve with parallel processing in Java. I checked executor framework in Java but I got stuck in it.

I thought one approach to implement my requirement.

I want to implement some fixed number of minimum objects will be processed by each thread so that each thread do its work and process objects in a quick manner. How can I acheive this ? Or If any other approach is ther for implementing my requirement pls share.

Eg:

List objects = new List();

For(Object object : objects) { //Doing some common operation for all Objects

}

like image 616
SSV Avatar asked Dec 06 '22 11:12

SSV


2 Answers

You can use a ThreadPoolExecutor, it will take care of load balance. Tasks will be distributed on different threads.

Here is an example:

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test {

    public static void main(String[] args) {

        // Fixed thread number
        ExecutorService service = Executors.newFixedThreadPool(10);

        // Or un fixed thread number
        // The number of threads will increase with tasks
        // ExecutorService service = Executors.newCachedThreadPool(10);

        List<Object> objects = new ArrayList<>();
        for (Object o : objects) {
            service.execute(new MyTask(o));
        }

        // shutdown
        // this will get blocked until all task finish
        service.shutdown();
        try {
            service.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static class MyTask implements Runnable {
        Object target;

        public MyTask(Object target) {
            this.target = target;
        }

        @Override
        public void run() {
            // business logic at here
        }
    }
}
like image 96
xingbin Avatar answered Dec 11 '22 11:12

xingbin


There are many options for processing a list in parallel:

Use a parallel stream:

objects.stream().parallel().forEach(object -> {
    //Your work on each object goes here, using object
})

Use an executor service to submit tasks if you want to use a pool with more threads than the fork-join pool:

ExecutorService es = Executors.newFixedThreadPool(10);
for(Object o: objects) {
    es.submit(() -> {
        //code here using Object o...
    }
}

This preceding example is essentially the same as the traditional executor service, running tasks on separate threads.

As an alternative to these, you can also submit using the completable future:

//You can also just run a for-each and manually add each
//feature to a list
List<CompletableFuture<Void>> futures = 
    objects.stream().map(object -> CompletableFuture.runAsync(() -> {
    //Your work on each object goes here, using object
})

You can then use the futures object to check the status of each execution if that's required.

like image 31
ernest_k Avatar answered Dec 11 '22 09:12

ernest_k