0

Trying to speed up the process of validation the items using stream api. Here is my code:

List<VodEpisodesDatum> allEpisodes = vodEpisodesSteps.getAllV1VodEpisodesFullSync(syncStop, null);
List<VodEpisodesDatum> movieEpisodes = vodEpisodesSteps.getAllV1VodEpisodesFullSync(syncStop, props);

List<VodEpisodesDatum> notMovieEpisodesTemp = new ArrayList<>();
List<VodEpisodesDatum> movieEpisodesTemp = new ArrayList<>();
List<VodEpisodesDatum> allHandledEpisodes = new ArrayList<>();

int amountToValidate = 100;

Collections.nCopies(amountToValidate, 1).parallelStream()
        .map(i -> RandomUtil.randomInteger.apply(allEpisodes.size()))
        .forEach(rand -> {
    if(isNotMovie.test(allEpisodes.get(rand))) notMovieEpisodesTemp.add(allEpisodes.get(rand));
    if(isMovie.test(allEpisodes.get(rand))) movieEpisodesTemp.add(allEpisodes.get(rand));
    allHandledEpisodes.add(allEpisodes.get(rand));
});

public class RandomUtil {
    public static IntFunction<Integer> randomInteger = r -> (int) ((Math.random() * (r - 1)));
}

But from time to time when I run this block of code i'm getting allHandledEpisodes != amountToValidate. How can it be??? It looks like a some kind of magic. I suspect that .map() does not work as expected here, but I don't really understand what's going on.

Naman
  • 27,789
  • 26
  • 218
  • 353
Dmitry
  • 93
  • 1
  • 4
  • What is the expectation here, are you comparing the size of `allHandledEpisodes` with `amountToValidate`? Where is that significant code which is failing? – Naman Jun 13 '21 at 07:59
  • 2
    It is not the random parameter that is causing the problem. ArrayList is not thread safe and concurrent `add` is not predictable. Does this answer your question? [Concurrent threads adding to ArrayList at same time - what happens?](https://stackoverflow.com/questions/2715983/concurrent-threads-adding-to-arraylist-at-same-time-what-happens) – Gautham M Jun 13 '21 at 11:55
  • @GauthamM Yes, you were right, this is the thing that I didn't take into consideration. Adding `Collections.synchronizedList(new ArrayList<>());` resolved the issue completely. Thank you so much! – Dmitry Jun 14 '21 at 10:16

0 Answers0