简体   繁体   中英

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.

However, no breakpoints in the lambda expression's body gets hit.

Plus, I don't understand what this code does. AFAIU, the lambda expression does not return any values. Hence, the code does not supply any arguments to Thread constructor.

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?

The reason that no breakpoints get reached inside the lambda is because the lambda never gets run.

 () -> { 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() ). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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