Java threads are lazy versus fusion versus short circuit

I am trying to build a coco-like and consistent understanding of the application of lazy evaluation in the Java threading API.

Here is what I now understand:

  • Items
  • consumed only when necessary, that is, threads are lazy, and intermediate operations are lazy, such as, for example, a filter, will be filtered only when needed.
  • intermediate operations can be combined together (if they are stateless).
  • Short circuit operations do not require processing of the entire stream.

I want to make all these ideas together and make sure that I am not distorting anything. I find it difficult because whenever I read any literature on Java threads, she continues to say that they are lazy or use a lazy evaluation, and then very much interchangeably starts talking about optimization, such as merging and short circuiting.

So, would I say the following correctly?

  • fusion is how a lazy score was implemented in the streaming API, that is, an element is consumed, and operations are fused together when possible. I think that if the merger did not exist, then, of course, we returned to an impatient assessment, since the alternative would be to simply process all the elements for each intermediate operation before moving on to the next?

  • , ?

.

+5
1

. map:

.map(x -> x.squash())

Map

, ( ). :

.filter(x -> x.getColor() != YELLOW)

Filter

, ( - ). :

.forEach(System.out::println)

Display

. , :

.map(x -> x.squash())
.filter(x -> x.getColor() != YELLOW)
.forEach(System.out::println)

Fuse

Consumer, . , - , . . Fusion . ( , , , ). .

+36

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


All Articles