package pool;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
public class PoolDemo {
public static void main(String[] args) {
// 参数不是初始值,而是最大值
ExecutorService executorService = Executors.newFixedThreadPool(10);
// 向下转型,因为要使用ThreadPoolExecutor的方法操作线程
ThreadPoolExecutor pool = (ThreadPoolExecutor) executorService;
System.out.println(pool.getPoolSize());//0
// 添加线程
executorService.submit(()->{
System.out.println(Thread.currentThread().getName()+"hhhhhhh");
});
executorService.submit(()->{
System.out.println(Thread.currentThread().getName()+"hhhhhh");
});
// getPoolSize可以获得线程池里面的线程数量
System.out.println(pool.getPoolSize());//2
}
}
【笔记】ExecutorService,线程池定义的一个接口,可以定义线程池容量
最新推荐文章于 2024-04-20 16:51:21 发布
关键词由CSDN通过智能技术生成