Hutool工具包:使用ThreadUtil创建并使用线程池的简单案例
需求:模拟一个含有100个元素的集合,同时开启5个线程进行遍历。代码:package com.provy.jiagou;import cn.hutool.core.thread.ThreadUtil;import cn.hutool.db.Db;import cn.hu
需求:
模拟一个含有100个元素的集合,同时开启5个线程进行遍历。
代码:
package com.provy.jiagou; import cn.hutool.core.thread.ThreadUtil; import cn.hutool.db.Db; import cn.hutool.db.Entity; import java.sql.SQLException; import java.util.List; import java.util.concurrent.ExecutorService; /** * @author 架构师小跟班 * @Description: https://www.jiagou1216.com * @date 2020/7/7 18:22 */ public class Test { /** * 初始化线程池,同时执行5个线程 */ private static ExecutorService executor = ThreadUtil.newExecutor(5); public static void main(String[] args) { for (int i = 0; i < 100; i++) { //执行线程(jdk1.8版本以上写法) executor.execute(() -> handler()); } executor.shutdown(); } public static void handler() { //打印当前线程名字 System.out.println("当前执行线程:" + Thread.currentThread().getName()); } }
打印:
当前执行线程:pool-1-thread-1
当前执行线程:pool-1-thread-5
当前执行线程:pool-1-thread-4
当前执行线程:pool-1-thread-3
当前执行线程:pool-1-thread-3
当前执行线程:pool-1-thread-3
当前执行线程:pool-1-thread-3
当前执行线程:pool-1-thread-2
当前执行线程:pool-1-thread-2
当前执行线程:pool-1-thread-3
当前执行线程:pool-1-thread-3
当前执行线程:pool-1-thread-4
当前执行线程:pool-1-thread-5
当前执行线程:pool-1-thread-5
当前执行线程:pool-1-thread-1
当前执行线程:pool-1-thread-5
当前执行线程:pool-1-thread-5
当前执行线程:pool-1-thread-5
当前执行线程:pool-1-thread-4
当前执行线程:pool-1-thread-3
当前执行线程:pool-1-thread-2
当前执行线程:pool-1-thread-3
当前执行线程:pool-1-thread-4
当前执行线程:pool-1-thread-4
当前执行线程:pool-1-thread-5
当前执行线程:pool-1-thread-5
当前执行线程:pool-1-thread-1
当前执行线程:pool-1-thread-5
当前执行线程:pool-1-thread-5
……
很赞哦! (
)
相关文章
- hutool工具包:emoji表情符号转为字符符号(emoji-java)
- hutool工具包:移除字符串中的emoji表情符号(emoji-java)
- hutool工具包:判断一个字符串中是否包含emoji表情符号(e
- hutool工具包:EmojiUtil工具类报错问题(emoji-java)
- hutool工具包:使用DB类任意执行一条sql语句
- hutool工具包:使用DB类修改数据库某条数据
- hutool工具包:使用DB类向数据库插入一条数据
- hutool工具包:数据库DB操作之Entity转实体类
- Hutool工具包:使用BeanUtil复制Bean对象属性
- Hutool工具包:线程等待随机时间(ThreadUtil,RandomUtil)