mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -19,13 +19,13 @@ public class CronTimer extends Thread implements Serializable {
|
||||
private static final Log log = LogFactory.get();
|
||||
|
||||
/** 定时单元:秒 */
|
||||
private long TIMER_UNIT_SECOND = DateUnit.SECOND.getMillis();
|
||||
private final long TIMER_UNIT_SECOND = DateUnit.SECOND.getMillis();
|
||||
/** 定时单元:分 */
|
||||
private long TIMER_UNIT_MINUTE = DateUnit.MINUTE.getMillis();
|
||||
private final long TIMER_UNIT_MINUTE = DateUnit.MINUTE.getMillis();
|
||||
|
||||
/** 定时任务是否已经被强制关闭 */
|
||||
private boolean isStoped;
|
||||
private Scheduler scheduler;
|
||||
private boolean isStop;
|
||||
private final Scheduler scheduler;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
@@ -42,7 +42,7 @@ public class CronTimer extends Thread implements Serializable {
|
||||
long thisTime = System.currentTimeMillis();
|
||||
long nextTime;
|
||||
long sleep;
|
||||
while(false == isStoped){
|
||||
while(false == isStop){
|
||||
//下一时间计算是按照上一个执行点开始时间计算的
|
||||
nextTime = ((thisTime / timerUnit) + 1) * timerUnit;
|
||||
sleep = nextTime - System.currentTimeMillis();
|
||||
@@ -62,7 +62,7 @@ public class CronTimer extends Thread implements Serializable {
|
||||
* 关闭定时器
|
||||
*/
|
||||
synchronized public void stopTimer() {
|
||||
this.isStoped = true;
|
||||
this.isStop = true;
|
||||
ThreadUtil.interrupt(this, true);
|
||||
}
|
||||
|
||||
|
@@ -55,7 +55,7 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
public class Scheduler implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Lock lock = new ReentrantLock();
|
||||
private final Lock lock = new ReentrantLock();
|
||||
|
||||
/** 时区 */
|
||||
private TimeZone timezone;
|
||||
|
@@ -11,8 +11,8 @@ import cn.hutool.cron.task.Task;
|
||||
*/
|
||||
public class TaskExecutor implements Runnable {
|
||||
|
||||
private Scheduler scheduler;
|
||||
private Task task;
|
||||
private final Scheduler scheduler;
|
||||
private final Task task;
|
||||
|
||||
/**
|
||||
* 获得任务对象
|
||||
|
@@ -10,8 +10,8 @@ package cn.hutool.cron;
|
||||
*/
|
||||
public class TaskLauncher implements Runnable{
|
||||
|
||||
private Scheduler scheduler;
|
||||
private long millis;
|
||||
private final Scheduler scheduler;
|
||||
private final long millis;
|
||||
|
||||
public TaskLauncher(Scheduler scheduler, long millis) {
|
||||
this.scheduler = scheduler;
|
||||
|
@@ -1,5 +1,8 @@
|
||||
package cn.hutool.cron;
|
||||
|
||||
import cn.hutool.cron.pattern.CronPattern;
|
||||
import cn.hutool.cron.task.Task;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
@@ -9,9 +12,6 @@ import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
|
||||
import cn.hutool.cron.pattern.CronPattern;
|
||||
import cn.hutool.cron.task.Task;
|
||||
|
||||
/**
|
||||
* 定时任务表<br>
|
||||
* 任务表将ID、表达式、任务一一对应,定时任务执行过程中,会周期性检查定时任务表中的所有任务表达式匹配情况,从而执行其对应的任务<br>
|
||||
@@ -22,14 +22,14 @@ import cn.hutool.cron.task.Task;
|
||||
public class TaskTable implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private ReadWriteLock lock = new ReentrantReadWriteLock();
|
||||
private final ReadWriteLock lock = new ReentrantReadWriteLock();
|
||||
|
||||
private Scheduler scheduler;
|
||||
private TimeZone timezone;
|
||||
private final Scheduler scheduler;
|
||||
private final TimeZone timezone;
|
||||
|
||||
private List<String> ids = new ArrayList<>();
|
||||
private List<CronPattern> patterns = new ArrayList<>();
|
||||
private List<Task> tasks = new ArrayList<>();
|
||||
private final List<String> ids = new ArrayList<>();
|
||||
private final List<CronPattern> patterns = new ArrayList<>();
|
||||
private final List<Task> tasks = new ArrayList<>();
|
||||
private int size;
|
||||
|
||||
/**
|
||||
|
@@ -1,11 +1,5 @@
|
||||
package cn.hutool.cron.pattern;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.List;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.cron.CronException;
|
||||
@@ -22,6 +16,12 @@ import cn.hutool.cron.pattern.parser.SecondValueParser;
|
||||
import cn.hutool.cron.pattern.parser.ValueParser;
|
||||
import cn.hutool.cron.pattern.parser.YearValueParser;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.List;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* 定时任务表达式<br>
|
||||
* 表达式类似于Linux的crontab表达式,表达式使用空格分成5个部分,按顺序依次为:
|
||||
@@ -90,22 +90,22 @@ public class CronPattern {
|
||||
private static final ValueParser DAY_OF_WEEK_VALUE_PARSER = new DayOfWeekValueParser();
|
||||
private static final ValueParser YEAR_VALUE_PARSER = new YearValueParser();
|
||||
|
||||
private String pattern;
|
||||
private final String pattern;
|
||||
|
||||
/** 秒字段匹配列表 */
|
||||
private List<ValueMatcher> secondMatchers = new ArrayList<>();
|
||||
private final List<ValueMatcher> secondMatchers = new ArrayList<>();
|
||||
/** 分字段匹配列表 */
|
||||
private List<ValueMatcher> minuteMatchers = new ArrayList<>();
|
||||
private final List<ValueMatcher> minuteMatchers = new ArrayList<>();
|
||||
/** 时字段匹配列表 */
|
||||
private List<ValueMatcher> hourMatchers = new ArrayList<>();
|
||||
private final List<ValueMatcher> hourMatchers = new ArrayList<>();
|
||||
/** 每月几号字段匹配列表 */
|
||||
private List<ValueMatcher> dayOfMonthMatchers = new ArrayList<>();
|
||||
private final List<ValueMatcher> dayOfMonthMatchers = new ArrayList<>();
|
||||
/** 月字段匹配列表 */
|
||||
private List<ValueMatcher> monthMatchers = new ArrayList<>();
|
||||
private final List<ValueMatcher> monthMatchers = new ArrayList<>();
|
||||
/** 星期字段匹配列表 */
|
||||
private List<ValueMatcher> dayOfWeekMatchers = new ArrayList<>();
|
||||
private final List<ValueMatcher> dayOfWeekMatchers = new ArrayList<>();
|
||||
/** 年字段匹配列表 */
|
||||
private List<ValueMatcher> yearMatchers = new ArrayList<>();
|
||||
private final List<ValueMatcher> yearMatchers = new ArrayList<>();
|
||||
/** 匹配器个数,取决于复合任务表达式中的单一表达式个数 */
|
||||
private int matcherSize;
|
||||
|
||||
|
@@ -10,7 +10,7 @@ import java.util.List;
|
||||
*/
|
||||
public class YearValueMatcher implements ValueMatcher{
|
||||
|
||||
private List<Integer> valueList;
|
||||
private final List<Integer> valueList;
|
||||
|
||||
public YearValueMatcher(List<Integer> intValueList) {
|
||||
this.valueList = intValueList;
|
||||
|
@@ -1,7 +1,5 @@
|
||||
package cn.hutool.cron.task;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.util.ClassLoaderUtil;
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
@@ -9,6 +7,8 @@ import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.cron.CronException;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* 反射执行任务<br>
|
||||
* 通过传入类名#方法名,通过反射执行相应的方法<br>
|
||||
@@ -19,8 +19,8 @@ import cn.hutool.cron.CronException;
|
||||
*/
|
||||
public class InvokeTask implements Task{
|
||||
|
||||
private Object obj;
|
||||
private Method method;
|
||||
private final Object obj;
|
||||
private final Method method;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
|
@@ -6,7 +6,7 @@ package cn.hutool.cron.task;
|
||||
*
|
||||
*/
|
||||
public class RunnableTask implements Task{
|
||||
private Runnable runnable;
|
||||
private final Runnable runnable;
|
||||
|
||||
public RunnableTask(Runnable runnable) {
|
||||
this.runnable = runnable;
|
||||
|
@@ -10,23 +10,12 @@ public class AddAndRemoveMainTest {
|
||||
CronUtil.setMatchSecond(true);
|
||||
CronUtil.start(false);
|
||||
CronUtil.getScheduler().clear();
|
||||
String id = CronUtil.schedule("*/2 * * * * *", new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Console.log("task running : 2s");
|
||||
}
|
||||
});
|
||||
String id = CronUtil.schedule("*/2 * * * * *", (Runnable) () -> Console.log("task running : 2s"));
|
||||
ThreadUtil.sleep(3000);
|
||||
CronUtil.remove(id);
|
||||
Console.log("Task Removed");
|
||||
id = CronUtil.schedule("*/3 * * * * *", new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Console.log("New task add running : 3s");
|
||||
}
|
||||
});
|
||||
CronUtil.schedule("*/3 * * * * *", (Runnable) () -> Console.log("New task add running : 3s"));
|
||||
Console.log("New Task added.");
|
||||
}
|
||||
}
|
||||
|
@@ -13,7 +13,7 @@ import cn.hutool.core.util.IdUtil;
|
||||
*/
|
||||
public class TestJob {
|
||||
|
||||
private String jobId = IdUtil.simpleUUID();
|
||||
private final String jobId = IdUtil.simpleUUID();
|
||||
|
||||
/**
|
||||
* 执行定时任务内容
|
||||
|
Reference in New Issue
Block a user