add method

This commit is contained in:
Looly 2021-08-26 11:49:52 +08:00
parent 2deb6e0fe4
commit b88de146f6
3 changed files with 44 additions and 13 deletions

View File

@ -3,7 +3,7 @@
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.7.10 (2021-08-25) # 5.7.10 (2021-08-26)
### 🐣新特性 ### 🐣新特性
* 【core 】 增加NamingCase类 * 【core 】 增加NamingCase类
@ -21,6 +21,7 @@
* 【core 】 XmlUtil增加beanToXml重载支持忽略null * 【core 】 XmlUtil增加beanToXml重载支持忽略null
* 【core 】 添加NullComparator、FuncComparatorissue#I471X7@Gitee * 【core 】 添加NullComparator、FuncComparatorissue#I471X7@Gitee
* 【core 】 LambdaUtil添加getFieldNameissue#I4750U@Gitee * 【core 】 LambdaUtil添加getFieldNameissue#I4750U@Gitee
* 【cron 】 Scheduler增加setThreadExecutorissue#I47A6N@Gitee
### 🐞Bug修复 ### 🐞Bug修复
* 【core 】 修复MapUtil.sort比较器不一致返回原map的问题issue#I46AQJ@Gitee * 【core 】 修复MapUtil.sort比较器不一致返回原map的问题issue#I46AQJ@Gitee

View File

@ -1,6 +1,7 @@
package cn.hutool.core.util; package cn.hutool.core.util;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateField; import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
@ -418,7 +419,7 @@ public class RandomUtil {
*/ */
public static <T> List<T> randomEleList(List<T> source, int count) { public static <T> List<T> randomEleList(List<T> source, int count) {
if (count >= source.size()) { if (count >= source.size()) {
return source; return ListUtil.toList(source);
} }
final int[] randomList = ArrayUtil.sub(randomInts(source.size()), 0, count); final int[] randomList = ArrayUtil.sub(randomInts(source.size()), 0, count);
List<T> result = new ArrayList<>(); List<T> result = new ArrayList<>();

View File

@ -100,7 +100,8 @@ public class Scheduler implements Serializable {
/** /**
* 设置是否为守护线程<br> * 设置是否为守护线程<br>
* 如果为true则在调用{@link #stop()}方法后执行的定时任务立即结束否则等待执行完毕才结束默认非守护线程 * 如果为true则在调用{@link #stop()}方法后执行的定时任务立即结束否则等待执行完毕才结束默认非守护线程<br>
* 如果用户调用{@link #setThreadExecutor(ExecutorService)}自定义线程池则此参数无效
* *
* @param on {@code true}为守护线程否则非守护线程 * @param on {@code true}为守护线程否则非守护线程
* @return this * @return this
@ -109,9 +110,7 @@ public class Scheduler implements Serializable {
public Scheduler setDaemon(boolean on) throws CronException { public Scheduler setDaemon(boolean on) throws CronException {
lock.lock(); lock.lock();
try { try {
if (this.started) { checkStarted();
throw new CronException("Scheduler already started!");
}
this.daemon = on; this.daemon = on;
} finally { } finally {
lock.unlock(); lock.unlock();
@ -119,6 +118,26 @@ public class Scheduler implements Serializable {
return this; return this;
} }
/**
* 设置自定义线程池<br>
* 自定义线程池时须考虑方法执行的线程是否为守护线程
*
* @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 class Scheduler implements Serializable {
public Scheduler start() { public Scheduler start() {
lock.lock(); lock.lock();
try { try {
if (this.started) { checkStarted();
throw new CronException("Schedule is started!");
}
// 无界线程池确保每一个需要执行的线程都可以及时运行同时复用已有线程避免线程重复创建 if(null != this.threadExecutor){
this.threadExecutor = ExecutorBuilder.create().useSynchronousQueue().setThreadFactory(// // 无界线程池确保每一个需要执行的线程都可以及时运行同时复用已有线程避免线程重复创建
ThreadFactoryBuilder.create().setNamePrefix("hutool-cron-").setDaemon(this.daemon).build()// this.threadExecutor = ExecutorBuilder.create().useSynchronousQueue().setThreadFactory(//
).build(); ThreadFactoryBuilder.create().setNamePrefix("hutool-cron-").setDaemon(this.daemon).build()//
).build();
}
this.taskLauncherManager = new TaskLauncherManager(this); this.taskLauncherManager = new TaskLauncherManager(this);
this.taskExecutorManager = new TaskExecutorManager(this); this.taskExecutorManager = new TaskExecutorManager(this);
@ -445,4 +464,14 @@ public class Scheduler implements Serializable {
return this; return this;
} }
/**
* 检查定时任务是否已经启动
*
* @throws CronException 已经启动则抛出此异常
*/
private void checkStarted() throws CronException{
if (this.started) {
throw new CronException("Scheduler already started!");
}
}
} }