diff --git a/hutool-cron/src/main/java/cn/hutool/v7/cron/TaskLauncher.java b/hutool-cron/src/main/java/cn/hutool/v7/cron/TaskLauncher.java
index 1f7685209..bdbfad513 100644
--- a/hutool-cron/src/main/java/cn/hutool/v7/cron/TaskLauncher.java
+++ b/hutool-cron/src/main/java/cn/hutool/v7/cron/TaskLauncher.java
@@ -23,16 +23,18 @@ package cn.hutool.v7.cron;
*
* @author Looly
* @param scheduler 调度器
- * @param millis 毫秒数
+ * @param millis 触发事件的时间戳
*/
public record TaskLauncher(Scheduler scheduler, long millis) implements Runnable {
@Override
public void run() {
- //匹配秒部分由用户定义决定,始终不匹配年
- scheduler.taskTable.executeTaskIfMatch(this.scheduler, this.millis);
-
- //结束通知
- scheduler.taskManager.notifyLauncherCompleted(this);
+ try{
+ //匹配秒部分由用户定义决定,始终不匹配年
+ scheduler.taskTable.executeTaskIfMatch(this.scheduler, this.millis);
+ } finally {
+ //结束通知
+ scheduler.taskManager.notifyLauncherCompleted(this);
+ }
}
}
diff --git a/hutool-cron/src/main/java/cn/hutool/v7/cron/TaskManager.java b/hutool-cron/src/main/java/cn/hutool/v7/cron/TaskManager.java
index 11310cfdc..88cb1e44b 100644
--- a/hutool-cron/src/main/java/cn/hutool/v7/cron/TaskManager.java
+++ b/hutool-cron/src/main/java/cn/hutool/v7/cron/TaskManager.java
@@ -17,20 +17,21 @@
package cn.hutool.v7.cron;
import cn.hutool.v7.core.collection.ListUtil;
+import cn.hutool.v7.core.date.DateUtil;
import cn.hutool.v7.cron.task.CronTask;
import cn.hutool.v7.cron.task.Task;
+import cn.hutool.v7.log.Log;
import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
/**
* 任务管理器,提供任务的全生命周期管理,提供:
*
- * - 启动器管理
- * - 执行器管理
+ * - 启动器管理,由CronTimer按照固定周期(每分钟或每秒钟)调用,用于检查CronTable中的任务是否匹配。
+ * - 执行器管理,由CronTable匹配后调用,用于启动具体的任务。
*
*
* @author Looly
@@ -73,7 +74,7 @@ public class TaskManager implements Serializable {
/**
* 启动 TaskLauncher
*
- * @param millis 触发事件的毫秒数
+ * @param millis 触发事件的时间戳
* @return {@link TaskLauncher}
*/
protected TaskLauncher spawnLauncher(final long millis) {
@@ -115,7 +116,7 @@ public class TaskManager implements Serializable {
* @param task {@link Task}
* @return {@link TaskExecutor}
*/
- public TaskExecutor spawnExecutor(final CronTask task) {
+ protected TaskExecutor spawnExecutor(final CronTask task) {
final TaskExecutor executor = new TaskExecutor(this.scheduler, task);
synchronized (this.executors) {
this.executors.add(executor);
@@ -129,7 +130,7 @@ public class TaskManager implements Serializable {
*
* @param executor 执行器 {@link TaskExecutor}
*/
- public void notifyExecutorCompleted(final TaskExecutor executor) {
+ protected void notifyExecutorCompleted(final TaskExecutor executor) {
synchronized (executors) {
executors.remove(executor);
}
diff --git a/hutool-cron/src/main/java/cn/hutool/v7/cron/TaskTable.java b/hutool-cron/src/main/java/cn/hutool/v7/cron/TaskTable.java
index bb8f076f8..5cd2645de 100644
--- a/hutool-cron/src/main/java/cn/hutool/v7/cron/TaskTable.java
+++ b/hutool-cron/src/main/java/cn/hutool/v7/cron/TaskTable.java
@@ -303,7 +303,7 @@ public class TaskTable implements Serializable {
* @param millis 时间毫秒
* @since 3.1.1
*/
- protected void executeTaskIfMatchInternal(final Scheduler scheduler, final long millis) {
+ private void executeTaskIfMatchInternal(final Scheduler scheduler, final long millis) {
final int size = size();
for (int i = 0; i < size; i++) {
if (this.table.getMiddle(i).match(scheduler.config.timezone, millis, scheduler.config.matchSecond)) {
diff --git a/hutool-cron/src/test/java/cn/hutool/v7/cron/demo/CronTest.java b/hutool-cron/src/test/java/cn/hutool/v7/cron/demo/CronTest.java
index 8b0d88966..a3542a834 100644
--- a/hutool-cron/src/test/java/cn/hutool/v7/cron/demo/CronTest.java
+++ b/hutool-cron/src/test/java/cn/hutool/v7/cron/demo/CronTest.java
@@ -21,7 +21,6 @@ import cn.hutool.v7.core.thread.ThreadUtil;
import cn.hutool.v7.cron.CronUtil;
import cn.hutool.v7.cron.TaskExecutor;
import cn.hutool.v7.cron.listener.TaskListener;
-import cn.hutool.v7.cron.task.Task;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@@ -33,7 +32,7 @@ public class CronTest {
@Test
@Disabled
public void customCronTest() {
- CronUtil.schedule("*/2 * * * * *", (Task) () -> Console.log("Task executed."));
+ CronUtil.schedule("*/2 * * * * *", () -> Console.log("Task executed."));
// 支持秒级别定时任务
CronUtil.setMatchSecond(true);