Why the below code doesn\'t print any output whereas if we remove parallel, it prints 0, 1?
IntStream.iterate(0, i -> ( i + 1 ) % 2)
.parallel()
I know the code is not correct and as suggested in the solution too, if we move the limit before distinct, we won't have infinite loop.
Parallel function is using fork and join concept to allocate the work, which allocates all the available thread for the work, rather than single thread.
We are rightly expecting infinite loop, as multiple thread is infinitely processing a data and nothing stopping them, as the limit of 10 is never hitting after distinct.
It might be possible that it keeps trying to fork and never tries to join to move it forward. But still I believe its a defect in the java with more than anything else.