Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
closeable.close();
logger.info("Close closeable.");
executorPool.shutdown();
logger.info("Shutdown executorPool");
}
}));
答案 0 :(得分:12)
只需将new Runnable()
替换为() ->
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
closeable.close();
logger.info("Close closeable.");
executorPool.shutdown();
logger.info("Shutdown executorPool");
}));
Runnable是一个功能接口,这意味着它只有一个抽象方法,因此它可以替换为lambda表达式,这是一种功能,可以作为参数传递