4

This is my code:

    list.stream()
        .parallel()
        .map(cpcP -> this.myDao.myOperation(cip, cpcP))
        .collect(Collectors.toList());

Inside myOperation I've written a log:

log.debug("{} -> {}", Thread.currentThread().getName(), ...);

I'm getting those logs:

ForkJoinPool.commonPool-worker-3 -> ...
ForkJoinPool.commonPool-worker-3 -> ...
ForkJoinPool.commonPool-worker-3 -> ...
ForkJoinPool.commonPool-worker-3 -> ...
ForkJoinPool.commonPool-worker-3 -> ...
ForkJoinPool.commonPool-worker-3 -> ...
ForkJoinPool.commonPool-worker-3 -> ...
ForkJoinPool.commonPool-worker-3 -> ...
...

It seems it is always using the same thread.

Any ideas?

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
Jordi
  • 20,868
  • 39
  • 149
  • 333
  • How about list.parallelStream()? – dreamcrash Feb 23 '21 at 14:50
  • The stream api I believe will "detect" whether the stream is actually large enough to warrant dividing into multiple streams, or can be completed quickly by a single thread. I don't know the criteria though. – markspace Feb 23 '21 at 14:50
  • 1
    @markspace it doesn’t detect but if the initiating thread completes its work before the other threads even picked up their work the first thread will continue with another chunk so in the most extreme case it may complete all the work before the other threads managed to contribute. Another possible scenario is the source list having limited parallel processing capabilities, e.g. when using a `LinkedList`. – Holger Feb 23 '21 at 14:53
  • The latter might be what I was thinking of, thanks for the clarification @Holger – markspace Feb 23 '21 at 14:54
  • 2
    Other possibility: the other worker threads are busy with other parallel operations. – Holger Feb 23 '21 at 14:57
  • 4
    parallel streams are using the `ForkJoinPool.common()`, so it could very well be that there are other usages of that common pool in your application and you only get a single thread for your operation. – Lino Feb 23 '21 at 14:58
  • Yep @Lino has a good point – dreamcrash Feb 23 '21 at 15:07
  • If you want to specify your own `ForkJoinPool` you can have a look at this other question: [Custom thread pool in Java 8 parallel stream](https://stackoverflow.com/q/21163108/5515060) – Lino Feb 23 '21 at 15:15

1 Answers1

0

Aside the comments that indicate the common thread pool could be busy with other operations in parallel, a couple of other reasons:

  1. you have the system property java.util.concurrent.ForkJoinPool.common.parallelism to 1, explicitly limiting the parallelism of the pool to 1.

  2. The number of cores is very small (either 1 or 2), since the common pool uses Runtime.getRuntime().availableProcessors() - 1 as the parallelism.

M A
  • 71,713
  • 13
  • 134
  • 174