mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add method
This commit is contained in:
@@ -100,7 +100,8 @@ public class Scheduler implements Serializable {
|
||||
|
||||
/**
|
||||
* 设置是否为守护线程<br>
|
||||
* 如果为true,则在调用{@link #stop()}方法后执行的定时任务立即结束,否则等待执行完毕才结束。默认非守护线程
|
||||
* 如果为true,则在调用{@link #stop()}方法后执行的定时任务立即结束,否则等待执行完毕才结束。默认非守护线程<br>
|
||||
* 如果用户调用{@link #setThreadExecutor(ExecutorService)}自定义线程池则此参数无效
|
||||
*
|
||||
* @param on {@code true}为守护线程,否则非守护线程
|
||||
* @return this
|
||||
@@ -109,9 +110,7 @@ public class Scheduler implements Serializable {
|
||||
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 class Scheduler implements Serializable {
|
||||
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() {
|
||||
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 class Scheduler implements Serializable {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查定时任务是否已经启动
|
||||
*
|
||||
* @throws CronException 已经启动则抛出此异常
|
||||
*/
|
||||
private void checkStarted() throws CronException{
|
||||
if (this.started) {
|
||||
throw new CronException("Scheduler already started!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user