//Executors 工具类,三大方法
public class Demo01 {
public static void main(String[] args) {
//ExecutorService threadPool = Executors.newSingleThreadExecutor();//单个线程池
//ExecutorService threadPool = Executors.newFixedThreadPool(5);//创建一个固定的线程池大小:5个
ExecutorService threadPool = Executors.newCachedThreadPool();//缓存,可伸缩的池
try {
for (int i = 0; i < 10; i++) {
//使用线程池创建线程
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName()+" OK");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//线程池用完需关闭
threadPool.shutdown();
}
}
}
Executors 工具类,三大方法
最新推荐文章于 2024-03-28 23:33:16 发布