What is the difference between state expression and stateless lambda expression?

According to the OCP book, stateful operations, which are also called a state lambda expression, should be avoided. The definition given in the book is an expression with an explicit expression of the state, which depends on any state that may change during the execution of the pipeline. ''

They give an example where a parallel stream is used to add a fixed set of numbers to a synchronized ArrayList using a function .map().

The order in the arraylist is completely random, and this should make the person see that an expression with an explicit expression in a state gives unpredictable results at runtime. Therefore, it is strongly recommended that you avoid stateful operations when using parallel threads in order to eliminate any potential side effects of the data.

They do not display the lambda expression without regard to the state, which provides a solution to the same problem (adding a number to the synchronized arraylist), and I still do not understand that the problem is using the map function to fill in an empty synchronized arraylist with data ... What is there a condition that can change during pipeline execution? Do they relate to the Arraialist himself? For example, when another thread decides to add other data to the ArrayList, when the parallel thread is still in the process, adding numbers and thereby changing the final result?

Perhaps someone can provide me with a better example that shows what an expression of a lambda state is and why it should be avoided. That would be very appreciated.

thank

+4
4

, Consumer ( : ):

public static class StatefulConsumer implements IntConsumer {

    private static final Integer ARBITRARY_THRESHOLD = 10;
    private boolean flag = false;
    private final List<Integer> list = new ArrayList<>();

    @Override
    public void accept(int value) {
        if(flag){   // exit condition
            return; 
        }
        if(value >= ARBITRARY_THRESHOLD){
            flag = true;
        }
        list.add(value); 
    }

}

, List ( , ) ( ).

, .

, , , , , Stream, .

, , , .

+1

:

 List<Integer> list = new ArrayList<>();

    List<Integer> result = Stream.of(1, 2, 3, 4, 5, 6)
            .parallel()
            .map(x -> {
                list.add(x);
                return x;
            })
            .collect(Collectors.toList());

System.out.println(list);

, , , ArrayList.

:

  List<Integer> list = Collections.synchronizedList(new ArrayList<>());

, list . . , , ( ArrayList), .

, list , -, . result : [1, 2, 3, 4, 5, 6] .

stateful; , synchronized List, :

 Stream.of(1, 2, 3, 4, 5, 6)
            .filter(x -> x > 2) // for example a filter is present
            .collect(Collectors.collectingAndThen(Collectors.toList(), 
                          Collections::synchronizedList));
+1

, :

public static void main(String[] args) {

Set<Integer> seen = new HashSet<>();

IntStream stream = IntStream.of(1, 2, 3, 1, 2, 3);

// Stateful lambda expression
IntUnaryOperator mapUniqueLambda = (int i) -> {
    if (!seen.contains(i)) {
        seen.add(i);
        return i;
    }
    else {
        return 0;
    }
};

int sum = stream.parallel().map(mapUniqueLambda).peek(i ->   System.out.println("Stream member: " + i)).sum();

System.out.println("Sum: " + sum);
}

, , :

Stream member: 1
Stream member: 0
Stream member: 2
Stream member: 3
Stream member: 1
Stream member: 2
Sum: 9

9 , ?
: IntStream , 1 2 .

0

- , , . , - , , .

: OCP: Java SE 8 Programmer II: 1Z0-809 by Jeanne Boyarsky, Scott Selikoff

    List < Integer > data = Collections.synchronizedList(new ArrayList < > ());

            Arrays.asList(1, 2, 3, 4, 5, 6, 7).parallelStream()


                   .map(i -> {
                    data.add(i);
                    return i;
                }) // AVOID STATEFUL LAMBDA EXPRESSIONS!
                .forEachOrdered(i -> System.out.print(i+" "));


            System.out.println();
            for (int e: data) {
                System.out.print(e + " ");

:

1 2 3 4 5 6 7 
1 7 5 2 3 4 6 

, . , , , .

0
source

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


All Articles