-4

I need to speed up my program and tried this by changing a .stream() to a .parallelStream().

cars is a list of car objects. I use the id of each car to create a JsonCurrent and then use this jsonCurrent to add a new element to to carList.

When I use .stream() this works just fine but with .parallelStream() it doesn't work. The debugger shows me that all fields of JsonCurrent are null when carService.createJson() is called.

cars.parallelStream().forEach(car -> {
            JsonCurrent currentLocation = carPositionService.carPosition(car.getId());
            carList.add(carService.createJson(car, calendarWeek, currentWeek, currentLocation));
        });

.stream()

If cars has 60 objects, carList contains 60 objects like expected.

.parallelStream()

If cars has 60 objects, carList contains 36 objects.

Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56
Martin
  • 113
  • 5
  • 16

2 Answers2

2

If the List implementation referred by carList is not thread-safe, using a parallel stream is a bad idea to populate it.
If you collect the JsonCurrent objects into a List instead of adding in an existing list it should solve your issue. You could add all the elements of the collect list in carList in a second time such as :

List<JsonCurrent> collected = 
cars.parallelStream().map(car -> carService.createJson(car, calendarWeek, currentWeek, carPositionService.carPosition(car.getId())).collect(toList());     
carList.addAll(collected);

but note that the behavior will be different from a no parallel stream : the order of the insertion of elements could differ.
At last, concerning the performance difference, you should use JMH or any tool dedicated to that to deem the real difference between the two approaches.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
1

Parallel Streams can be used stateless, associative, and non-interfering operations i.e.

1) Stateless operation: The state of one element does not affect another element

2)  Non-interfering operation: Data source is not affected

3) Associative operation : Result is not affected by the order of operands

So, your carList must be threadsafe.