简体   繁体   中英

How do method references work with threads? `new Thread(foo::bar)`

i just try to create thread by using method reference.

new Thread(thisMonitor::method1).start();

so how to explain why it works.

does it works just like the lambda?

The Thread constructor you are using accepts a Runnable :

public Thread(Runnable target)

Runnable is a functional interface with a method that accepts no argument and returns no value:

public abstract void run()

If method1 is such a method, the method reference thisMonitor::method1 can serve as an implementation of Runnable .

And you can also use a lambda expression instead of the method reference:

new Thread(() -> thisMonitor.method1()).start();

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