简体   繁体   中英

How to parallelize loops with Java 8's Fork/Join framework

How to parallelize loops with Java 8's Fork/Join framework. Accually I did not work with multiple threading . I read lots of question in SO .Now i am unable to implement the parallel processing of list in Java 8. Any one can help me ?

I have tried somthing like from this link .

routes.stream().parallel().forEach(this::doSomething);

Scenario like list based on routes list I need to devide the task and execute I need like insted of foreach loop I want parallel execution of based on array size.

My problem is when processing the updateSchedules service it is taking too much time. That is the reason I want to implement the threading concept here. scheduleService.updateSchedules(originId, destinationId,req.getJourneyDate());

for (Availabilities ar : routes) {
  try {
    log.info("Starting for bus" + ar);
    Bus bus = new Bus();

    // Get schedule list
    BitlaSchedules schedule = scheduleRepo
      .findByOriginIdAndDestinationIdAndScheduleIdAndTravelIdAndRouteId(originId, 
         destinationId, ar.getScheduleId(), ar.getTravelId(), ar.getRouteId());

    if (schedule == null) {
      scheduleService.updateSchedules(originId, destinationId,req.getJourneyDate());
      schedule = scheduleRepo
        .findByOriginIdAndDestinationIdAndScheduleIdAndTravelIdAndRouteId(originId, 
          destinationId, ar.getScheduleId(), ar.getTravelId(), ar.getRouteId());
    }
  } catch(Exception e) {
    log.error(e.getMessage());
  }
}

Probably the basic error is that you are trying to do it.

Don't! The fork/join framework is a very specific piece of engineering - which solves a very specific area: - solving CPU intensive problems; - that can be split without sharing resources (ie no synchronization or locking between ).

Your code seems to use an external service: - if the service uses database of any kind, then your problem is not CPU intensive; - even if not, then - since there is an obvious update , then there is a shared, mutable state that requires synchronization (especially since we seem to have multiple writers).

This means that you gain nothing by using the parallel stream.

Just use a standard executor with a thread pool and submit your items as tasks.

As @fdreger already said, it will only help you with CPU intensive tasks. So before making any assumptions WHY something should be run parallel to gain performance, do yourself a favor and profile. Most of the time the bottleneck is IO related.

I will give you a very simple example how you could use parallel streams in java.

public class Test {

    public static void main(String[] args) {
        // some dummy data
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 20; ++i) list.add(i);

        // to simulate some CPU intensive work
        Random random = new SecureRandom();

        List<String> result = list.parallelStream().map(i -> {

            // simulate work load
            int millis = 0;
            try {
                millis = random.nextInt(1000);
                Thread.sleep(millis);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }

            // return any desired result
            return "Done something with " + i + " in thread " + Thread.currentThread().getName() + " took " + millis + "ms";
        }).collect(Collectors.toList()); // collect joins - will return once all the workers are done

        // print the result
        result.forEach(System.out::println);    
    }
}

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