RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 625153
Accepted
TimurVI
TimurVI
Asked:2020-02-08 21:37:01 +0000 UTC2020-02-08 21:37:01 +0000 UTC 2020-02-08 21:37:01 +0000 UTC

在 lambda 表达式中捕获值

  • 772

Lambda 表达式应该用于捕获值,而不是变量。捕获值会鼓励您编写没有副作用的代码,因为替代方案更难。

在lambda表达式中捕获值是什么意思?

java
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Rostislav Dugin
    2020-02-08T21:46:10Z2020-02-08T21:46:10Z

    大概,作者的意思是lambda表达式不应该接触变量,而是将一个值作为输入,在输出处赋值。例如:

    很糟糕:

    final String string = "string";
    class.method(() -> string += "abc");
    

    好的:

    class.method((string) -> string += "abc");
    
    • 8
  2. Best Answer
    Regent
    2020-02-23T22:05:41Z2020-02-23T22:05:41Z

    这意味着你应该在 lambda 表达式中使用外部(相对于表达式)不可变的值,而不是值和内部状态可以改变的外部变量。外部不可变值分别意味着有效的最终局部变量和原始类型的字段,以及有效的内部状态不会改变的最终对象。

    这是因为 Streams 和 lambda 表达式被设计为多线程的。

    使用变量 ( counter) 而不是值的问题在此示例中可见:

    private static class Element
    {
        private final int value;
    
        public Element(int value) { this.value = value; }
    
        public int getValue() { return value; }
    }
    
    private static volatile int counter = 0;
    
    public static void main(String[] args)
    {
        List<Element> list = new ArrayList<>();
        for (int i = 0; i < 100 * 1000; i++)
        {
            list.add(new Element(1));
        }
        list.parallelStream().forEach(e -> counter += e.getValue());
        System.out.println(counter);
    }
    

    不必指望值会显示在屏幕上100000,因为存在竞争条件。在我的测试中,这段代码在 100,000 次中只能得到 299 次正确值。

    这就是为什么在 lambda 表达式中使用的局部变量必须是有效最终变量的原因之一。代码有效性

    int localCounter = 0;
    list.parallelStream().forEach(e -> localCounter += e.getValue());
    

    会导致局部变量的竞争条件,这将是Java多线程编程的新一轮问题。局部变量被认为是线程安全的,Java 开发人员不想打破这一原则。

    您可以像这样欺骗编译器来限制有效的最终值:

    int[] localCounter = { 0 };
    list.parallelStream().forEach(e -> localCounter[0] += e.getValue());
    System.out.println(localCounter[0]);
    

    因此,在使用有效的 final 局部变量时,仍有可能“搬起石头砸自己的脚”。当然,您不应该对该值再次计算错误感到惊讶。实际上,这绝对不是要走的路。

    是的,你可以在这里使用AtomicInteger:

    AtomicInteger atomicInteger = new AtomicInteger();
    list.parallelStream().forEach(e -> atomicInteger.addAndGet(e.getValue()));
    System.out.println(atomicInteger.get());
    

    然而,这扼杀了代码并行化的整个思想。

    在这种情况下,假定使用一堆mapand reduce:

    int localCounter = list.parallelStream().map(e -> e.getValue()).reduce(0, (a, b) -> a + b);
    System.out.println(localCounter);
    

    map有和的部分reduce也可以这样写:

    .map(Element::getValue).reduce(0, Integer::sum)
    

    可以在此处阅读 Brian Goetz(《Java 并发实践》的作者)关于此的文章。


    然而,捕获变量而不是值的问题不仅仅发生在并行执行中。例如:

    private static class Element
    {
        public int x;
    
        public Element(int x) { this.x = x; }
    
        public Function<Integer, Integer> getMapper()
        {
            return (e -> e + x);
        }
    }
    
    public static void main(String[] args)
    {
        Element element = new Element(2);
        List<Integer> list1 = Arrays.asList(10, 20, 30);
        Function<Integer, Integer> function1 = element.getMapper();
        element.x = 4;
        List<Integer> list2 = Arrays.asList(10, 20, 30);
        Function<Integer, Integer> function2 = element.getMapper();
        list1 = list1.stream().map(function1).collect(Collectors.toList());
        list2 = list2.stream().map(function2).collect(Collectors.toList());
        System.out.println(list1);
        System.out.println(list2);
    }
    

    在此代码中,捕获了一个变量(不是有效的最终字段)x,因此,而不是预期的输出

    [12, 22, 32]
    [14, 24, 34]
    

    将被显示

    [14, 24, 34]
    [14, 24, 34]
    

    捕获相同值时:

    public Function<Integer, Integer> getMapper()
    {
        int n = x;
        return (e -> e + n);
    }
    

    不会有这样的问题/错误。

    • 8

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    Python 3.6 - 安装 MySQL (Windows)

    • 1 个回答
  • Marko Smith

    C++ 编写程序“计算单个岛屿”。填充一个二维数组 12x12 0 和 1

    • 2 个回答
  • Marko Smith

    返回指针的函数

    • 1 个回答
  • Marko Smith

    我使用 django 管理面板添加图像,但它没有显示

    • 1 个回答
  • Marko Smith

    这些条目是什么意思,它们的完整等效项是什么样的

    • 2 个回答
  • Marko Smith

    浏览器仍然缓存文件数据

    • 1 个回答
  • Marko Smith

    在 Excel VBA 中激活工作表的问题

    • 3 个回答
  • Marko Smith

    为什么内置类型中包含复数而小数不包含?

    • 2 个回答
  • Marko Smith

    获得唯一途径

    • 3 个回答
  • Marko Smith

    告诉我一个像幻灯片一样创建滚动的库

    • 1 个回答
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Алексей Шиманский 如何以及通过什么方式来查找 Javascript 代码中的错误? 2020-08-03 00:21:37 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5