java8 函数式接口作为传参,参数可以使用方法代替
示例:
public class TestFunctionalInterface {
@Test
public void testSubmit() {
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.submit(new Runnable() {
public void run() {
System.out.println("Asynchronous task");
}
});
}
@Test
public void testFunctionalMethod() {
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.submit(this::doSth);
}
private void doSth() {
System.out.println("doSth");
}
}
分析:
1.定义传参为函数式接口的方法
2.定义函数式接口
3.使用,调用传参为函数式接口的方法
presto ,facebook airlift源码中有很多这种使用