简体   繁体   English

断点不适用于Java中的Lambda表达式

[英]Breakpoints does not work in lambda expression in Java

I cannot understand what the following code does: 我不明白以下代码的作用:

public class Main {

    public static void main(String[] args) throws UnsupportedProtocolException, IOException {
        new Thread(() -> {
            PropertiesLoader loader = new PropertiesLoader();
            loader.load(args);
            System.out.println(loader.getProperties());
        });
    }
}

I have put breakpoints in all lines of the main method but only the breakpoint at the line starting with new Thread gets hit. 我已经在main方法的所有行中都设置了断点,但是只有命中new Thread的行的断点才被命中。

However, no breakpoints in the lambda expression's body gets hit. 但是,lambda表达式主体中的任何断点都不会受到攻击。

Plus, I don't understand what this code does. 另外,我不明白这段代码的作用。 AFAIU, the lambda expression does not return any values. AFAIU,lambda表达式不返回任何值。 Hence, the code does not supply any arguments to Thread constructor. 因此,该代码不向Thread构造函数提供任何参数。

Plus, I don't understand why is there a thread creation here. 另外,我不明白为什么在这里创建线程。

Could you help me on how to reach inside of the lambda expression using breakpoints? 您能帮助我如何使用断点到达lambda表达式的内部吗?

The reason that no breakpoints get reached inside the lambda is because the lambda never gets run. lambda内部没有达到断点的原因是因为lambda从未运行。

 () -> { PropertiesLoader loader = new PropertiesLoader(); loader.load(args); System.out.println(loader.getProperties()); } 

is a lambda taking no arguments and having a void return type, which matches the functional interface java.lang.Runnable (because it has a method void run() ). 是不带任何参数且具有void返回类型的lambda,它与功能接口java.lang.Runnable匹配(因为它具有方法void run() )。 The constructor java.lang.Thread(java.lang.Runnable) is called to construct a new thread, but start() is never called on that thread, hence the lambda is never run. 构造函数java.lang.Thread(java.lang.Runnable)被调用以构造一个新线程,但是从不对该线程调用start() ,因此lambda永远不会运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM