如何创建线程池?
2025-02-19
NEW个对象
1、通过ExecutorService
ExecutorService executor = Executors.newCachedThreadPool();
ExecutorService executor = Executors.newFixedThreadPool(nThreads);
ExecutorService executor = Executors.newSingleThreadExecutor();
ScheduledExecutorService executor = Executors.newScheduledThreadPool(corePoolSize);
2、使用 ThreadPoolExecutor 自定义线程池
ExecutorService executor = new ThreadPoolExecutor(
corePoolSize(20),
maximumPoolSize(30),
keepAliveTime(60L),
unit(TimeUnit.SECONDS),
new LinkedBlockingQueue<Runnable>()
);
3、执行任务
executor.submit(() -> {
// 任务逻辑
});
上一篇:链表反转
下一篇:java代码,随机打乱一个数组
相关文章
-
java中a=a+b和a+=b真的一样吗?
这是一道笔试题,说实话我就是再复习两年,也不会复习到这个题。 既然遇到了,就了解清楚吧:
NEW个对象 2025-01-13
-
接口和抽象类的区别?
接口和抽象类都是用来定义对象的公共行为的,两者本身不能实例化,但二者有以下7点不同:
NEW个对象 2025-01-10
-
数据库出现死锁的案例
报告生成案例: 报告编辑的时候:更新模板内容,更新报告引用的id。 报告生成的时候:更新报告引用的id,再更新模板内容。
NEW个对象 2025-08-01