Java 8 thread processing is not irrevocable

I have a problem with Java 8 threads where data is being processed in unexpected amounts, not when they are requested. I have a rather complicated thread stream, which must be parallel, because I use concatto merge two streams.

My problem is related to the fact that the data is apparently analyzed in large volumes of minutes, and sometimes in hours. I expect this processing to happen as soon as Streamit reads the incoming data to spread the workload. Mass processing seems contradictory in almost all ways.

So, the question is, why does this massive collection arise and how can I avoid it.

My input is an unknown size Spliterator, and I use forEach as a terminal operation.

+1
source share
1 answer

This is a fundamental principle of parallel threads that the order of meetings should not match the order of processing. This allows you to simultaneously process elements of subscriptions or subtrees when assembling a correctly ordered result, if necessary. This explicitly allows bulk processing and even makes it mandatory for parallel processing of ordered flows.

This behavior is determined by the specific implementation of the implementation of Spliterators trySplit. The specification says:

If this is a Spliterator ORDERED, the returned Spliterator should cover the strict prefix of the elements

...

API Note:

trySplit ( ) , .


​​ , , . / ?

, . , . . , , , .

. , , , .

, addAll .


, , , , . ORDERED, . , , , , AbstractSpliterator, . , , split .

, .

Stream.generate(()->{
    LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(1));
    return Thread.currentThread().getName();
}).parallel().forEach(System.out::println);

.

+5

Source: https://habr.com/ru/post/1663897/


All Articles