Lambda表达式的形式
(parameters) -> { statements; }
parameters:参数列表,类似于方法参数。
->:箭头符号,用于分隔参数列表和Lambda表达式的主体。
{ statements; }:Lambda表达式的主体部分,类似方法体。
使用场景:
函数式接口可以使用Lambda表达式来创建接口实例。函数式接口是指只包含一个抽象方法的接口。如Runnable。
@FunctionalInterface
public interface Runnable {
/**
* When an object implementing interface {@code Runnable} is used
* to create a thread, starting the thread causes the object's
* {@code run} method to be called in that separately executing
* thread.
* <p>
* The general contract of the method {@code run} is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run()
*/
public abstract void run();
}
提交一个Runnable实例到线程池。
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
3,
3,
3,
TimeUnit.MINUTES,
new ArrayBlockingQueue<>(3)
);
threadPoolExecutor.submit(()->{
System.out.println("1111111111");
});