Java线程池和Lambda表达式演示
package com.zr;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/*
线程池和Lambda表达式演示
*/
public class DemoThreadPoolAndLambda {
public static void main(String[] args) {
//创建连接池对象,有2个线程
ExecutorService threadPool = Executors.newFixedThreadPool(2);
//未使用Lambda的代码
/*
threadPool.submit(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"线程");
}
});
*/
//用submit方法开启线程池,并使用Lambda表达式简化代码
threadPool.submit(()->System.out.println(Thread.currentThread().getName()+"线程一"));
threadPool.submit(()-> System.out.println(Thread.currentThread().getName()+"线程二"));
//关闭线程池
threadPool.shutdown();
}
}