diff --git a/CHANGELOG.md b/CHANGELOG.md index a5721997f4..e5be1053e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ------------------------------------------------------------------------------------------------------------- -# 5.7.10 (2021-08-25) +# 5.7.10 (2021-08-26) ### 🐣新特性 * 【core 】 增加NamingCase类 @@ -21,6 +21,7 @@ * 【core 】 XmlUtil增加beanToXml重载,支持忽略null * 【core 】 添加NullComparator、FuncComparator(issue#I471X7@Gitee) * 【core 】 LambdaUtil添加getFieldName(issue#I4750U@Gitee) +* 【cron 】 Scheduler增加setThreadExecutor(issue#I47A6N@Gitee) ### 🐞Bug修复 * 【core 】 修复MapUtil.sort比较器不一致返回原map的问题(issue#I46AQJ@Gitee) diff --git a/hutool-core/src/main/java/cn/hutool/core/util/RandomUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/RandomUtil.java index 0d0a9ffa64..b9a91db0ec 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/RandomUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/RandomUtil.java @@ -1,6 +1,7 @@ package cn.hutool.core.util; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.collection.ListUtil; import cn.hutool.core.date.DateField; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; @@ -418,7 +419,7 @@ public static List randomEles(List list, int count) { */ public static List randomEleList(List source, int count) { if (count >= source.size()) { - return source; + return ListUtil.toList(source); } final int[] randomList = ArrayUtil.sub(randomInts(source.size()), 0, count); List result = new ArrayList<>(); diff --git a/hutool-cron/src/main/java/cn/hutool/cron/Scheduler.java b/hutool-cron/src/main/java/cn/hutool/cron/Scheduler.java index e47b5714e1..3618be18e2 100644 --- a/hutool-cron/src/main/java/cn/hutool/cron/Scheduler.java +++ b/hutool-cron/src/main/java/cn/hutool/cron/Scheduler.java @@ -100,7 +100,8 @@ public TimeZone getTimeZone() { /** * 设置是否为守护线程
- * 如果为true,则在调用{@link #stop()}方法后执行的定时任务立即结束,否则等待执行完毕才结束。默认非守护线程 + * 如果为true,则在调用{@link #stop()}方法后执行的定时任务立即结束,否则等待执行完毕才结束。默认非守护线程
+ * 如果用户调用{@link #setThreadExecutor(ExecutorService)}自定义线程池则此参数无效 * * @param on {@code true}为守护线程,否则非守护线程 * @return this @@ -109,9 +110,7 @@ public TimeZone getTimeZone() { public Scheduler setDaemon(boolean on) throws CronException { lock.lock(); try { - if (this.started) { - throw new CronException("Scheduler already started!"); - } + checkStarted(); this.daemon = on; } finally { lock.unlock(); @@ -119,6 +118,26 @@ public Scheduler setDaemon(boolean on) throws CronException { return this; } + /** + * 设置自定义线程池
+ * 自定义线程池时须考虑方法执行的线程是否为守护线程 + * + * @param threadExecutor 自定义线程池 + * @return this + * @throws CronException 定时任务已经启动抛出此异常 + * @since 5.7.10 + */ + public Scheduler setThreadExecutor(ExecutorService threadExecutor) throws CronException { + lock.lock(); + try { + checkStarted(); + this.threadExecutor = threadExecutor; + } finally { + lock.unlock(); + } + return this; + } + /** * 是否为守护线程 * @@ -376,14 +395,14 @@ public Scheduler start(boolean isDaemon) { public Scheduler start() { lock.lock(); try { - if (this.started) { - throw new CronException("Schedule is started!"); - } + checkStarted(); - // 无界线程池,确保每一个需要执行的线程都可以及时运行,同时复用已有线程避免线程重复创建 - this.threadExecutor = ExecutorBuilder.create().useSynchronousQueue().setThreadFactory(// - ThreadFactoryBuilder.create().setNamePrefix("hutool-cron-").setDaemon(this.daemon).build()// - ).build(); + if(null != this.threadExecutor){ + // 无界线程池,确保每一个需要执行的线程都可以及时运行,同时复用已有线程避免线程重复创建 + this.threadExecutor = ExecutorBuilder.create().useSynchronousQueue().setThreadFactory(// + ThreadFactoryBuilder.create().setNamePrefix("hutool-cron-").setDaemon(this.daemon).build()// + ).build(); + } this.taskLauncherManager = new TaskLauncherManager(this); this.taskExecutorManager = new TaskExecutorManager(this); @@ -445,4 +464,14 @@ public Scheduler stop(boolean clearTasks) { return this; } + /** + * 检查定时任务是否已经启动 + * + * @throws CronException 已经启动则抛出此异常 + */ + private void checkStarted() throws CronException{ + if (this.started) { + throw new CronException("Scheduler already started!"); + } + } }