简体   繁体   中英

Parallel Processing of DTO in Java 7

I have requirement where need to process and map the DTOs with the values in for loop as below. Each of the mapping method here consumes nearly 10 minutes to complete its business logic and hence creating performance delay. I am working to refine the algorithms of business logic. However, please let me know if each of these mapping methods can be parallel processed to increase performance.

Since application is compatible only with Java 7 I cannot use streams of java 8.

for(Portfolio pf : portfolio) {
   mapAddress(pf);
   mapBusinessUnit(pf);
   mapRelationShipDetails(pf)
   --
   --
   --
}

You could split portfolios to different threads using either Runnable or Callable .

For example:

public class PortfolioService implements Callable<List<Portfolio>>
{
    List<Portfolio> portfolios;
    public PortfolioService(List<Portfolio> portfolios)
    {
        this.portfolios = portfolios;
    }


    public List<Portfolio> call()
    {
    for(Portfolio pf : portfolios) {
       mapAddress(pf);
       mapBusinessUnit(pf);
        ...
        }   
     return portfolios;
    }
}

However, this needs some modifications in your main class. I am using Callable here, since I don't know if you want to do something with all of these mapped Portfolios afterwards. However, if you want to let the threads do all of the work and don't need any return, use Runnable and modify the code.

1) You have to get your amount of cores:

int threads = Runtime.getRuntime().availableProcessors();

2) Now you split the workload per thread

// determine the average workload per thread
int blocksize = portfolios.size()/threads;
// doesn't always get all entries 
int overlap = portfolios.size()%threads;

3) Start an ExecutorService, make a list of Future Elements, make reminder variable for old index of array slice

ExecutorService exs = Executors.newFixedThreadPool(threads);
List<Future<List<Portfoilio>>> futures = new ArrayList();
int oldIndex = 0;

4) Start threads

for(int i = 0; i<threads; i++)
{
    int actualBlocksize = blocksize;
    if(overlap != 0){
    actualBlocksize++;
    overlap--;
    }
    futures.add(exs.submit(new PortfolioService(portfolios.subList(oldIndex,actualBlocksize));
    oldIndex = actualBlocksize;
}

5) Shutdown the ExecutorService and await it's termination

exs.shutdown();
try {exs.awaitTermination(6, TimeUnit.HOURS);}
catch (InterruptedException e) { }

6) do something with the future, if you want / have to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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