有状态和无状态lambda表达式之间有什么区别? [英] what is the difference between a stateful and a stateless lambda expression?

查看:148
本文介绍了有状态和无状态lambda表达式之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据OCP的书,人们必须避免有状态的操作,否则称为有状态的lambda表达式。本书中提供的定义是有状态的lambda表达式,其结果取决于在执行管道期间可能发生变化的任何状态。

According to the OCP book one must avoid stateful operations otherwise known as stateful lambda expression. The definition provided in the book is 'a stateful lambda expression is one whose result depends on any state that might change during the execution of a pipeline.'

它们提供了一个示例其中并行流用于使用 .map()函数将固定的数字集合添加到同步的ArrayList中。

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

arraylist中的顺序是完全随机的,这应该让人看到有状态的lambda表达式在运行时产生不可预测的结果。这就是为什么强烈建议在使用并行流时避免有状态操作以消除任何潜在的数据副作用。

The order in the arraylist is completely random and this should make one see that a stateful lambda expression produces unpredictable results in runtime. That's why its strongly recommended to avoid stateful operations when using parallel streams so as to remove any potential data side effects.

它们没有显示无状态lambda表达式,它提供了解决相同的问题(向同步的arraylist添加数字),我仍然不知道使用map函数用数据填充空的同步arraylist的问题是什么...在这期间可能会发生什么变化的状态执行管道?他们指的是Arraylist本身吗?就像当另一个线程决定将其他数据添加到ArrayList时,当并行流仍然在添加数字并因此改变最终结果的过程中?

They don't show a stateless lambda expression that provides a solution to the same problem (adding numbers to a synchronized arraylist) and I still don't get what the problem is with using a map function to populate an empty synchronized arraylist with data... What is exactly the state that might change during the execution of a pipeline? Are they referring to the Arraylist itself? Like when another thread decides to add other data to the ArrayList when the parallelstream is still in the process adding the numbers and thus altering the eventual result?

也许有人可以提供给我有一个更好的例子,它显示了有状态的lambda表达式是什么以及为什么要避免它。非常感谢。

Maybe someone can provide me with a better example that shows what a stateful lambda expression is and why it should be avoided. That would be very much appreciated.

谢谢

推荐答案

试着给出一个例子,让我们考虑下面的消费者(注意:这个函数的用处不在这里):

To try to give an example, let's consider the following Consumer (note : the usefulness of such a function is not of the matter here) :

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); 
    }

}

这是一个会添加商品的消费者到列表(让我们不考虑如何取回列表或线程安全)并有一个标志(代表有状态)。

It's a consumer that will add items to a List (let's not consider how to get back the list nor the thread safety) and has a flag (to represent the statefulness).

这背后的逻辑是,一旦达到阈值,消费者应该停止添加项目。

The logic behind this would be that once the threshold has been reached, the consumer should stop adding items.

你的书试图说的是因为没有保证顺序,函数将不得不消耗 Stream <的元素/ code>,输出是不确定的。

What your book was trying to say was that because there is no guaranteed order in which the function will have to consume the elements of the Stream, the output is non-deterministic.

因此,他们建议您只使用无状态函数,这意味着它们将始终使用相同的输入产生相同的结果。

Thus, they advise you to only use stateless functions, meaning they will always produce the same result with the same input.

这篇关于有状态和无状态lambda表达式之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆