Skip to content

Commit

Permalink
add method
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Aug 26, 2021
1 parent 2deb6e0 commit b88de14
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

-------------------------------------------------------------------------------------------------------------

# 5.7.10 (2021-08-25)
# 5.7.10 (2021-08-26)

### 🐣新特性
* 【core 】 增加NamingCase类
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -418,7 +419,7 @@ public static <T> List<T> randomEles(List<T> list, int count) {
*/
public static <T> List<T> randomEleList(List<T> source, int count) {
if (count >= source.size()) {
return source;
return ListUtil.toList(source);
}
final int[] randomList = ArrayUtil.sub(randomInts(source.size()), 0, count);
List<T> result = new ArrayList<>();
Expand Down
51 changes: 40 additions & 11 deletions hutool-cron/src/main/java/cn/hutool/cron/Scheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public TimeZone getTimeZone() {

/**
* 设置是否为守护线程<br>
* 如果为true,则在调用{@link #stop()}方法后执行的定时任务立即结束,否则等待执行完毕才结束。默认非守护线程
* 如果为true,则在调用{@link #stop()}方法后执行的定时任务立即结束,否则等待执行完毕才结束。默认非守护线程<br>
* 如果用户调用{@link #setThreadExecutor(ExecutorService)}自定义线程池则此参数无效
*
* @param on {@code true}为守护线程,否则非守护线程
* @return this
Expand All @@ -109,16 +110,34 @@ 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();
}
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;
}

/**
* 是否为守护线程
*
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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!");
}
}
}

0 comments on commit b88de14

Please sign in to comment.