格式调整

This commit is contained in:
lxy 2020-08-17 09:42:40 +08:00
parent 2a085daa2a
commit 080c6d22ee
6 changed files with 167 additions and 147 deletions

View File

@ -20,7 +20,7 @@ public class TimerExecutor {
start(); start();
} }
public void add(Task... task){ public void add(Task... task) {
for (Task t : task) { for (Task t : task) {
t.setTimerExecutor(this); t.setTimerExecutor(this);
queue.push(t); queue.push(t);
@ -28,24 +28,25 @@ public class TimerExecutor {
} }
} }
protected void add(Task task, boolean upTime){ protected void add(Task task, boolean upTime) {
task.setTimerExecutor(this); task.setTimerExecutor(this);
if (upTime) task.nextTime(); if (upTime) task.nextTime();
queue.push(task); queue.push(task);
} }
public Task remove(String name){ public Task remove(String name) {
return queue.remove(name); return queue.remove(name);
} }
public Task get(String name){
public Task get(String name) {
return queue.get(name); return queue.get(name);
} }
public void start() { public void start() {
new Thread(()->{ new Thread(() -> {
while (true){ while (true) {
try{ try {
Task take = null; Task take = null;
try { try {
take = queue.take(); take = queue.take();
@ -56,7 +57,7 @@ public class TimerExecutor {
//执行调度 //执行调度
executor.execute(take); executor.execute(take);
//add(take, true); //继续添加任务到 队列 //add(take, true); //继续添加任务到 队列
}catch (Exception e){ } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }

View File

@ -7,7 +7,6 @@ import net.tccn.timer.task.Task;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.util.Date; import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
@ -44,7 +43,7 @@ public class TimerTask implements Task {
} }
@Override @Override
public long nextTime(){ public long nextTime() {
LocalDateTime next = scheduled.nextTime(); LocalDateTime next = scheduled.nextTime();
this.theTime = Date.from(next.atZone(ZoneId.systemDefault()).toInstant()).getTime(); this.theTime = Date.from(next.atZone(ZoneId.systemDefault()).toInstant()).getTime();
@ -52,8 +51,9 @@ public class TimerTask implements Task {
System.out.println("下次执行:"+ sdf.format(next.toInstant(ZoneOffset.of("+8")).toEpochMilli()));*/ System.out.println("下次执行:"+ sdf.format(next.toInstant(ZoneOffset.of("+8")).toEpochMilli()));*/
return theTime; return theTime;
} }
@Override @Override
public long theTime(){ public long theTime() {
LocalDateTime next = scheduled.theTime(); LocalDateTime next = scheduled.theTime();
this.theTime = Date.from(next.atZone(ZoneId.systemDefault()).toInstant()).getTime(); this.theTime = Date.from(next.atZone(ZoneId.systemDefault()).toInstant()).getTime();
return theTime; return theTime;

View File

@ -9,29 +9,30 @@ import java.util.Set;
/** /**
* Created by liangxianyou at 2018/7/23 14:07. * Created by liangxianyou at 2018/7/23 14:07.
*/ */
public class TimerQueue{ public class TimerQueue {
Object lock = new Object(); Object lock = new Object();
Task[] queue = new Task[128]; Task[] queue = new Task[128];
Set<String> names = new HashSet<>(); Set<String> names = new HashSet<>();
int size=0; int size = 0;
/** /**
* 新加调度任务 * 新加调度任务
*
* @param task * @param task
*/ */
public void push(Task task) { public void push(Task task) {
synchronized (lock){ synchronized (lock) {
remove(task.getName()); remove(task.getName());
int inx = size;//目标坐标 int inx = size;//目标坐标
while (inx > 0 && queue[inx-1].theTime() > task.theTime()){ while (inx > 0 && queue[inx - 1].theTime() > task.theTime()) {
inx--; inx--;
} }
if (queue.length == size+1) if (queue.length == size + 1)
queue = Arrays.copyOf(queue, size * 2); queue = Arrays.copyOf(queue, size * 2);
for (int i = size+1; i > inx; i--) { for (int i = size + 1; i > inx; i--) {
queue[i] = queue[i-1]; queue[i] = queue[i - 1];
} }
queue[inx] = task; queue[inx] = task;
@ -43,25 +44,26 @@ public class TimerQueue{
/** /**
* 调度等待执行的任务 * 调度等待执行的任务
*
* @return * @return
* @throws InterruptedException * @throws InterruptedException
*/ */
public Task take() throws InterruptedException { public Task take() throws InterruptedException {
synchronized (lock){ synchronized (lock) {
while (size == 0) lock.wait(10);//循环避免非put线程唤醒空异常 while (size == 0) lock.wait(10);//循环避免非put线程唤醒空异常
long currentTime = System.currentTimeMillis(); long currentTime = System.currentTimeMillis();
long nextTime = queue[0].theTime(); long nextTime = queue[0].theTime();
if (currentTime >= nextTime){ if (currentTime >= nextTime) {
Task task = queue[0]; Task task = queue[0];
for (int i = 0; i < size;i++) { for (int i = 0; i < size; i++) {
queue[i] = queue[i+1]; queue[i] = queue[i + 1];
} }
queue[size-1] = null; queue[size - 1] = null;
size--; size--;
return task; return task;
}else { } else {
lock.wait(nextTime - currentTime); lock.wait(nextTime - currentTime);
return take(); return take();
} }
@ -70,34 +72,36 @@ public class TimerQueue{
/** /**
* 删除指定名称的任务 * 删除指定名称的任务
*
* @param name * @param name
* @return * @return
*/ */
public Task remove(String name){ public Task remove(String name) {
return get(name, true); return get(name, true);
} }
/** /**
* 返回指定名称的任务 * 返回指定名称的任务
*
* @param name * @param name
* @return * @return
*/ */
public Task get(String name){ public Task get(String name) {
return get(name, false); return get(name, false);
} }
private Task get(String name, boolean remove) { private Task get(String name, boolean remove) {
synchronized (lock){ synchronized (lock) {
if(!names.contains(name)) return null; if (!names.contains(name)) return null;
Task take = null; Task take = null;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
if (name.equals(queue[i].getName())){ if (name.equals(queue[i].getName())) {
take = queue[i]; take = queue[i];
if (!remove) break; if (!remove) break;
while (i < size+1){ while (i < size + 1) {
queue[i] = queue[i+1]; queue[i] = queue[i + 1];
queue[i+1] = null; queue[i + 1] = null;
i++; i++;
} }
names.remove(name); names.remove(name);

View File

@ -9,12 +9,14 @@ public interface Scheduled {
/** /**
* 下次执行时间 * 下次执行时间
*
* @return * @return
*/ */
LocalDateTime nextTime(); LocalDateTime nextTime();
/** /**
* 当前执行时间 * 当前执行时间
*
* @return * @return
*/ */
LocalDateTime theTime(); LocalDateTime theTime();

View File

@ -8,10 +8,11 @@ import java.util.List;
/** /**
* 时间解析器 * 时间解析器
*
* @author: liangxianyou * @author: liangxianyou
*/ */
@SuppressWarnings("Duplicates") @SuppressWarnings("Duplicates")
public class ScheduledExpres implements Scheduled{ public class ScheduledExpres implements Scheduled {
private int year; private int year;
private int month; private int month;
private int[] minutes; private int[] minutes;
@ -23,24 +24,27 @@ public class ScheduledExpres implements Scheduled{
private String cfg; private String cfg;
private String[] cfgArr; private String[] cfgArr;
private LocalDateTime theTime; private LocalDateTime theTime;
private int _y,_M,_d,_H,_m; private int _y, _M, _d, _H, _m;
@Deprecated @Deprecated
private ScheduledExpres(String cfg){ private ScheduledExpres(String cfg) {
this.cfg = cfg; this.cfg = cfg;
this.theTime = LocalDateTime.now(); this.theTime = LocalDateTime.now();
initTheTime(); initTheTime();
} }
@Deprecated @Deprecated
private ScheduledExpres(final LocalDateTime startTime, String cfg){ private ScheduledExpres(final LocalDateTime startTime, String cfg) {
LocalDateTime now = LocalDateTime.now(); LocalDateTime now = LocalDateTime.now();
this.theTime = now.isAfter(startTime)? now : startTime; this.theTime = now.isAfter(startTime) ? now : startTime;
this.cfg = cfg; this.cfg = cfg;
initTheTime(); initTheTime();
} }
public static Scheduled of(String cfg) { public static Scheduled of(String cfg) {
return new ScheduledExpres(cfg); return new ScheduledExpres(cfg);
} }
public static Scheduled of(final LocalDateTime startTime, String cfg) { public static Scheduled of(final LocalDateTime startTime, String cfg) {
return new ScheduledExpres(startTime, cfg); return new ScheduledExpres(startTime, cfg);
} }
@ -58,55 +62,56 @@ public class ScheduledExpres implements Scheduled{
setMinutes(); setMinutes();
_y = theTime.getYear(); _y = theTime.getYear();
_M= theTime.getMonthValue(); _M = theTime.getMonthValue();
_d= theTime.getDayOfMonth(); _d = theTime.getDayOfMonth();
_H= theTime.getHour(); _H = theTime.getHour();
_m= theTime.getMinute(); _m = theTime.getMinute();
String cmd = "";//y M d H m String cmd = "";//y M d H m
if (days.length == 0) cmd = "M"; if (days.length == 0) cmd = "M";
do { do {
carry(cmd); carry(cmd);
int inx; int inx;
if ((inx = nowOk(monthes, _M)) < 0){ if ((inx = nowOk(monthes, _M)) < 0) {
cmd = "y"; cmd = "y";
continue; continue;
} }
_M = monthes[inx]; _M = monthes[inx];
if ((inx = nowOk(days, _d)) < 0){ if ((inx = nowOk(days, _d)) < 0) {
cmd = "M"; cmd = "M";
continue; continue;
} }
_d = days[inx]; _d = days[inx];
if ((inx = nowOk(hours, _H)) < 0){ if ((inx = nowOk(hours, _H)) < 0) {
cmd = "d"; cmd = "d";
continue; continue;
} }
_H = hours[inx]; _H = hours[inx];
if ((inx = nowOk(minutes, _m)) < 0){ if ((inx = nowOk(minutes, _m)) < 0) {
cmd = "H"; cmd = "H";
continue; continue;
} }
_m = minutes[inx]; _m = minutes[inx];
break; break;
}while (true); } while (true);
theTime = LocalDateTime.of(_y, _M, _d, _H, _m); theTime = LocalDateTime.of(_y, _M, _d, _H, _m);
LocalDateTime now = LocalDateTime.now(); LocalDateTime now = LocalDateTime.now();
while (theTime.isBefore(now)){ while (theTime.isBefore(now)) {
theTime = carry("m"); theTime = carry("m");
} }
} }
/** /**
* 下一次执行的时间 * 下一次执行的时间
*
* @return * @return
*/ */
@Override @Override
public LocalDateTime nextTime(){ public LocalDateTime nextTime() {
return theTime = carry("m"); return theTime = carry("m");
} }
@ -117,17 +122,18 @@ public class ScheduledExpres implements Scheduled{
/** /**
* 通过发送指令进行进位 * 通过发送指令进行进位
*
* @param cmd 进位指令 * @param cmd 进位指令
*/ */
private LocalDateTime carry(String cmd){ private LocalDateTime carry(String cmd) {
int inx; int inx;
while (!"".equals(cmd)){ while (!"".equals(cmd)) {
switch (cmd) { switch (cmd) {
case "y": case "y":
_y = this.year = ++_y; _y = this.year = ++_y;
_M = this.month = monthes[0]; _M = this.month = monthes[0];
setDays(); setDays();
if (days.length == 0){ if (days.length == 0) {
cmd = "M"; cmd = "M";
continue; continue;
} }
@ -136,18 +142,18 @@ public class ScheduledExpres implements Scheduled{
_m = minutes[0]; _m = minutes[0];
break; break;
case "M": case "M":
if (_M < monthes[0]){ if (_M < monthes[0]) {
_M = monthes[0]; _M = monthes[0];
break; break;
} }
inx = Arrays.binarySearch(monthes, _M); inx = Arrays.binarySearch(monthes, _M);
if (inx < 0 || inx >= monthes.length-1) { if (inx < 0 || inx >= monthes.length - 1) {
cmd = "y"; cmd = "y";
continue ; continue;
} }
_M = this.month = monthes[inx+1]; _M = this.month = monthes[inx + 1];
setDays(); setDays();
if (days.length == 0){ if (days.length == 0) {
cmd = "M"; cmd = "M";
continue; continue;
} }
@ -156,43 +162,43 @@ public class ScheduledExpres implements Scheduled{
_m = minutes[0]; _m = minutes[0];
break; break;
case "d": case "d":
if (_d < days[0]){ if (_d < days[0]) {
_d = days[0]; _d = days[0];
break; break;
} }
inx = Arrays.binarySearch(days, _d); inx = Arrays.binarySearch(days, _d);
if (inx < 0 || inx >= days.length-1) { if (inx < 0 || inx >= days.length - 1) {
cmd = "M"; cmd = "M";
continue ; continue;
} }
_d = days[inx+1]; _d = days[inx + 1];
_H = hours[0]; _H = hours[0];
_m = minutes[0]; _m = minutes[0];
break; break;
case "H": case "H":
if (_H < hours[0]){ if (_H < hours[0]) {
_H = hours[0]; _H = hours[0];
break; break;
} }
inx = Arrays.binarySearch(hours, _H); inx = Arrays.binarySearch(hours, _H);
if (inx < 0 || inx >= hours.length -1) { if (inx < 0 || inx >= hours.length - 1) {
cmd = "d"; cmd = "d";
continue ; continue;
} }
_H = hours[inx+1]; _H = hours[inx + 1];
_m = minutes[0]; _m = minutes[0];
break; break;
case "m": case "m":
if (_m < minutes[0]){ if (_m < minutes[0]) {
_m = minutes[0]; _m = minutes[0];
break; break;
} }
inx = Arrays.binarySearch(minutes, _m); inx = Arrays.binarySearch(minutes, _m);
if (inx < 0 || inx >= minutes.length -1) { if (inx < 0 || inx >= minutes.length - 1) {
cmd = "H"; cmd = "H";
continue ; continue;
} }
_m = minutes[inx+1]; _m = minutes[inx + 1];
break; break;
} }
cmd = ""; cmd = "";
@ -202,21 +208,22 @@ public class ScheduledExpres implements Scheduled{
/** /**
* 得到初始合法时间的索引 * 得到初始合法时间的索引
*
* @param arr 合法时间序列 * @param arr 合法时间序列
* @param n 初始选中值 * @param n 初始选中值
* @return 合法时间的索引 * @return 合法时间的索引
*/ */
private int nowOk(int[] arr, int n){ private int nowOk(int[] arr, int n) {
if (arr == null || arr.length == 0) return -1; if (arr == null || arr.length == 0) return -1;
if (arr[0] > n) if (arr[0] > n)
return 0; return 0;
if (arr[arr.length-1] < n) if (arr[arr.length - 1] < n)
return 0; return 0;
if (arr[arr.length-1] == n) if (arr[arr.length - 1] == n)
return arr.length-1; return arr.length - 1;
for (int i = 0; i < arr.length-1; i++) { for (int i = 0; i < arr.length - 1; i++) {
if ((arr[i] < n && arr[i+1] > n) || arr[i] == n){ if ((arr[i] < n && arr[i + 1] > n) || arr[i] == n) {
return i; return i;
} }
} }
@ -224,237 +231,239 @@ public class ScheduledExpres implements Scheduled{
return -1; return -1;
} }
/** 以下为 初始化合法时间 weeksmonthesdayshourminutes 序列 */ /**
private void setMinutes(){ * 以下为 初始化合法时间 weeksmonthesdayshourminutes 序列
*/
private void setMinutes() {
String cfg = cfgArr[0]; String cfg = cfgArr[0];
if ("*".equals(cfg)){//* if ("*".equals(cfg)) {//*
minutes = new int[60]; minutes = new int[60];
for (int i = 0; i < 60; i++) { for (int i = 0; i < 60; i++) {
minutes[i] = i; minutes[i] = i;
} }
} else if (cfg.matches("^[0-5]??[0-9]??$")){//n } else if (cfg.matches("^[0-5]??[0-9]??$")) {//n
minutes = new int[1]; minutes = new int[1];
minutes[0] = Integer.parseInt(cfg); minutes[0] = Integer.parseInt(cfg);
}else if (cfg.matches("^[*]/[0-9]+$")){// */5 } else if (cfg.matches("^[*]/[0-9]+$")) {// */5
String[] strArr = cfg.split("/"); String[] strArr = cfg.split("/");
int p = Integer.parseInt(strArr[1]); int p = Integer.parseInt(strArr[1]);
minutes = new int[60/p]; minutes = new int[60 / p];
for (int i = 0; i < minutes.length; i++) { for (int i = 0; i < minutes.length; i++) {
minutes[i] = i*p; minutes[i] = i * p;
} }
}else if (cfg.matches("^([0-5]??[0-9]??,)+([0-5]??[0-9]??)?$")){//1,3 } else if (cfg.matches("^([0-5]??[0-9]??,)+([0-5]??[0-9]??)?$")) {//1,3
String[] strings = cfg.split(","); String[] strings = cfg.split(",");
minutes = new int[strings.length]; minutes = new int[strings.length];
for (int i = 0; i < strings.length; i++) { for (int i = 0; i < strings.length; i++) {
minutes[i] = Integer.parseInt(strings[i]); minutes[i] = Integer.parseInt(strings[i]);
} }
}else if (cfg.matches("^[0-5]??[0-9]??\\-[0-5]??[0-9]??$")){//1-3 } else if (cfg.matches("^[0-5]??[0-9]??\\-[0-5]??[0-9]??$")) {//1-3
String[] split = cfg.split("-"); String[] split = cfg.split("-");
int s = Integer.parseInt(split[0]); int s = Integer.parseInt(split[0]);
int e = Integer.parseInt(split[1]); int e = Integer.parseInt(split[1]);
minutes = new int[e-s +1]; minutes = new int[e - s + 1];
for (int i = 0; i < minutes.length; i++) { for (int i = 0; i < minutes.length; i++) {
minutes[i] = s+i; minutes[i] = s + i;
} }
}else if (cfg.matches("^[0-5]??[0-9]??\\-[0-5]??[0-9]??/[0-5]??[0-9]??$")){//3-18/5 } else if (cfg.matches("^[0-5]??[0-9]??\\-[0-5]??[0-9]??/[0-5]??[0-9]??$")) {//3-18/5
String[] strArr = cfg.split("/"); String[] strArr = cfg.split("/");
String[] str2Arr = strArr[0].split("-"); String[] str2Arr = strArr[0].split("-");
int s = Integer.parseInt(str2Arr[0]); int s = Integer.parseInt(str2Arr[0]);
int e = Integer.parseInt(str2Arr[1]); int e = Integer.parseInt(str2Arr[1]);
int p = Integer.parseInt(strArr[1]); int p = Integer.parseInt(strArr[1]);
minutes = new int[(e-s)/p]; minutes = new int[(e - s) / p];
for (int i = 0; i < minutes.length; i++) { for (int i = 0; i < minutes.length; i++) {
minutes[i] = s + i*p; minutes[i] = s + i * p;
} }
} }
} }
private void setHours(){ private void setHours() {
String cfg = cfgArr[1]; String cfg = cfgArr[1];
if ("*".equals(cfg)){//* if ("*".equals(cfg)) {//*
hours = new int[24]; hours = new int[24];
for (int i = 0; i < hours.length; i++) { for (int i = 0; i < hours.length; i++) {
hours[i] = i; hours[i] = i;
} }
} else if (cfg.matches("^[0-5]??[0-9]??$")){//n } else if (cfg.matches("^[0-5]??[0-9]??$")) {//n
hours = new int[1]; hours = new int[1];
hours[0] = Integer.parseInt(cfg); hours[0] = Integer.parseInt(cfg);
}else if (cfg.matches("^[*]/[0-9]+$")){// */5 } else if (cfg.matches("^[*]/[0-9]+$")) {// */5
String[] strArr = cfg.split("/"); String[] strArr = cfg.split("/");
int p = Integer.parseInt(strArr[1]); int p = Integer.parseInt(strArr[1]);
hours = new int[24/p]; hours = new int[24 / p];
for (int i = 0; i < hours.length; i++) { for (int i = 0; i < hours.length; i++) {
hours[i] = i*p; hours[i] = i * p;
} }
}else if (cfg.matches("^([0-5]??[0-9]??,)+([0-5]??[0-9]??)?$")){//1,3 } else if (cfg.matches("^([0-5]??[0-9]??,)+([0-5]??[0-9]??)?$")) {//1,3
String[] strArr = cfg.split(","); String[] strArr = cfg.split(",");
hours = new int[strArr.length]; hours = new int[strArr.length];
for (int i = 0; i < strArr.length; i++) { for (int i = 0; i < strArr.length; i++) {
hours[i] = Integer.parseInt(strArr[i]); hours[i] = Integer.parseInt(strArr[i]);
} }
}else if (cfg.matches("^[0-5]??[0-9]??\\-[0-5]??[0-9]??$")){//1-3 } else if (cfg.matches("^[0-5]??[0-9]??\\-[0-5]??[0-9]??$")) {//1-3
String[] split = cfg.split("-"); String[] split = cfg.split("-");
int s = Integer.parseInt(split[0]); int s = Integer.parseInt(split[0]);
int e = Integer.parseInt(split[1]); int e = Integer.parseInt(split[1]);
hours = new int[e-s +1]; hours = new int[e - s + 1];
for (int i = 0; i < hours.length; i++) { for (int i = 0; i < hours.length; i++) {
hours[i] = s+i; hours[i] = s + i;
} }
}else if (cfg.matches("^[0-5]??[0-9]??\\-[0-5]??[0-9]??/[0-5]??[0-9]??$")){//3-18/5 } else if (cfg.matches("^[0-5]??[0-9]??\\-[0-5]??[0-9]??/[0-5]??[0-9]??$")) {//3-18/5
String[] strArr = cfg.split("/"); String[] strArr = cfg.split("/");
String[] str2Arr = strArr[0].split("-"); String[] str2Arr = strArr[0].split("-");
int s = Integer.parseInt(str2Arr[0]); int s = Integer.parseInt(str2Arr[0]);
int e = Integer.parseInt(str2Arr[1]); int e = Integer.parseInt(str2Arr[1]);
int p = Integer.parseInt(strArr[1]); int p = Integer.parseInt(strArr[1]);
hours = new int[(e-s)/p]; hours = new int[(e - s) / p];
for (int i = 0; i < hours.length; i++) { for (int i = 0; i < hours.length; i++) {
hours[i] = s + i*p; hours[i] = s + i * p;
} }
} }
} }
private void setWeeks(){ private void setWeeks() {
String cfg = cfgArr[4]; String cfg = cfgArr[4];
if ("*".equals(cfg)){//* if ("*".equals(cfg)) {//*
weeks = new int[7]; weeks = new int[7];
for (int i = 0; i < weeks.length; i++) { for (int i = 0; i < weeks.length; i++) {
weeks[i] = i+1; weeks[i] = i + 1;
} }
} else if (cfg.matches("^[0-5]??[0-9]??$")){//n } else if (cfg.matches("^[0-5]??[0-9]??$")) {//n
weeks = new int[1]; weeks = new int[1];
weeks[0] = Integer.parseInt(cfg); weeks[0] = Integer.parseInt(cfg);
}else if (cfg.matches("^[*]/[0-9]+$")){// */5 } else if (cfg.matches("^[*]/[0-9]+$")) {// */5
String[] strArr = cfg.split("/"); String[] strArr = cfg.split("/");
int p = Integer.parseInt(strArr[1]); int p = Integer.parseInt(strArr[1]);
weeks = new int[7/p]; weeks = new int[7 / p];
for (int i = 0; i < weeks.length; i++) { for (int i = 0; i < weeks.length; i++) {
weeks[i] = i*p; weeks[i] = i * p;
} }
}else if (cfg.matches("^([0-5]??[0-9]??,)+([0-5]??[0-9]??)?$")){//1,3 } else if (cfg.matches("^([0-5]??[0-9]??,)+([0-5]??[0-9]??)?$")) {//1,3
String[] strArr = cfg.split(","); String[] strArr = cfg.split(",");
weeks = new int[strArr.length]; weeks = new int[strArr.length];
for (int i = 0; i < strArr.length; i++) { for (int i = 0; i < strArr.length; i++) {
weeks[i] = Integer.parseInt(strArr[i]); weeks[i] = Integer.parseInt(strArr[i]);
} }
}else if (cfg.matches("^[0-5]??[0-9]??\\-[0-5]??[0-9]??$")){//1-3 } else if (cfg.matches("^[0-5]??[0-9]??\\-[0-5]??[0-9]??$")) {//1-3
String[] split = cfg.split("-"); String[] split = cfg.split("-");
int s = Integer.parseInt(split[0]); int s = Integer.parseInt(split[0]);
int e = Integer.parseInt(split[1]); int e = Integer.parseInt(split[1]);
weeks = new int[e-s +1]; weeks = new int[e - s + 1];
for (int i = 0; i < weeks.length; i++) { for (int i = 0; i < weeks.length; i++) {
weeks[i] = s+i; weeks[i] = s + i;
} }
}else if (cfg.matches("^[0-5]??[0-9]??\\-[0-5]??[0-9]??/[0-5]??[0-9]??$")){//3-18/5 } else if (cfg.matches("^[0-5]??[0-9]??\\-[0-5]??[0-9]??/[0-5]??[0-9]??$")) {//3-18/5
String[] strArr = cfg.split("/"); String[] strArr = cfg.split("/");
String[] str2Arr = strArr[0].split("-"); String[] str2Arr = strArr[0].split("-");
int s = Integer.parseInt(str2Arr[0]); int s = Integer.parseInt(str2Arr[0]);
int e = Integer.parseInt(str2Arr[1]); int e = Integer.parseInt(str2Arr[1]);
int p = Integer.parseInt(strArr[1]); int p = Integer.parseInt(strArr[1]);
weeks = new int[(e-s)/p]; weeks = new int[(e - s) / p];
for (int i = 0; i < weeks.length; i++) { for (int i = 0; i < weeks.length; i++) {
weeks[i] = s + i*p; weeks[i] = s + i * p;
} }
} }
} }
private void setMonthes(){ private void setMonthes() {
String cfg = cfgArr[3]; String cfg = cfgArr[3];
if ("*".equals(cfg)){//* if ("*".equals(cfg)) {//*
monthes = new int[12]; monthes = new int[12];
for (int i = 0; i < monthes.length; i++) { for (int i = 0; i < monthes.length; i++) {
monthes[i] = i+1; monthes[i] = i + 1;
} }
} else if (cfg.matches("^[0-5]??[0-9]??$")){//n } else if (cfg.matches("^[0-5]??[0-9]??$")) {//n
monthes = new int[1]; monthes = new int[1];
monthes[0] = Integer.parseInt(cfg); monthes[0] = Integer.parseInt(cfg);
}else if (cfg.matches("^[*]/[0-9]+$")){// */5 } else if (cfg.matches("^[*]/[0-9]+$")) {// */5
String[] strArr = cfg.split("/"); String[] strArr = cfg.split("/");
int p = Integer.parseInt(strArr[1]); int p = Integer.parseInt(strArr[1]);
monthes = new int[12/p]; monthes = new int[12 / p];
for (int i = 0; i < monthes.length; i++) { for (int i = 0; i < monthes.length; i++) {
monthes[i] = i*p; monthes[i] = i * p;
} }
}else if (cfg.matches("^([0-5]??[0-9]??,)+([0-5]??[0-9]??)?$")){//1,3 } else if (cfg.matches("^([0-5]??[0-9]??,)+([0-5]??[0-9]??)?$")) {//1,3
String[] strArr = cfg.split(","); String[] strArr = cfg.split(",");
monthes = new int[strArr.length]; monthes = new int[strArr.length];
for (int i = 0; i < strArr.length; i++) { for (int i = 0; i < strArr.length; i++) {
monthes[i] = Integer.parseInt(strArr[i]); monthes[i] = Integer.parseInt(strArr[i]);
} }
}else if (cfg.matches("^[0-5]??[0-9]??\\-[0-5]??[0-9]??$")){//1-3 } else if (cfg.matches("^[0-5]??[0-9]??\\-[0-5]??[0-9]??$")) {//1-3
String[] split = cfg.split("-"); String[] split = cfg.split("-");
int s = Integer.parseInt(split[0]); int s = Integer.parseInt(split[0]);
int e = Integer.parseInt(split[1]); int e = Integer.parseInt(split[1]);
monthes = new int[e-s +1]; monthes = new int[e - s + 1];
for (int i = 0; i < monthes.length; i++) { for (int i = 0; i < monthes.length; i++) {
monthes[i] = s+i; monthes[i] = s + i;
} }
}else if (cfg.matches("^[0-5]??[0-9]??\\-[0-5]??[0-9]??/[0-5]??[0-9]??$")){//3-18/5 } else if (cfg.matches("^[0-5]??[0-9]??\\-[0-5]??[0-9]??/[0-5]??[0-9]??$")) {//3-18/5
String[] strArr = cfg.split("/"); String[] strArr = cfg.split("/");
String[] str2Arr = strArr[0].split("-"); String[] str2Arr = strArr[0].split("-");
int s = Integer.parseInt(str2Arr[0]); int s = Integer.parseInt(str2Arr[0]);
int e = Integer.parseInt(str2Arr[1]); int e = Integer.parseInt(str2Arr[1]);
int p = Integer.parseInt(strArr[1]); int p = Integer.parseInt(strArr[1]);
monthes = new int[(e-s)/p]; monthes = new int[(e - s) / p];
for (int i = 0; i < monthes.length; i++) { for (int i = 0; i < monthes.length; i++) {
monthes[i] = s + i*p; monthes[i] = s + i * p;
} }
} }
} }
private void setDays(){ private void setDays() {
String cfg = cfgArr[2]; String cfg = cfgArr[2];
//当前月份总天数 //当前月份总天数
LocalDate firstDay = LocalDate.of(year, month, 1); LocalDate firstDay = LocalDate.of(year, month, 1);
int lengthOfMonth = firstDay.lengthOfMonth(); int lengthOfMonth = firstDay.lengthOfMonth();
if ("*".equals(cfg)){//* if ("*".equals(cfg)) {//*
days = new int[lengthOfMonth]; days = new int[lengthOfMonth];
for (int i = 0; i < days.length; i++) { for (int i = 0; i < days.length; i++) {
days[i] = i+1; days[i] = i + 1;
} }
} else if (cfg.matches("^[0-5]??[0-9]??$")){//n } else if (cfg.matches("^[0-5]??[0-9]??$")) {//n
days = new int[1]; days = new int[1];
days[0] = Integer.parseInt(cfg); days[0] = Integer.parseInt(cfg);
}else if (cfg.matches("^[*]/[0-9]+$")){// */5 } else if (cfg.matches("^[*]/[0-9]+$")) {// */5
String[] strArr = cfg.split("/"); String[] strArr = cfg.split("/");
int p = Integer.parseInt(strArr[1]); int p = Integer.parseInt(strArr[1]);
days = new int[lengthOfMonth/p]; days = new int[lengthOfMonth / p];
for (int i = 0; i < days.length; i++) { for (int i = 0; i < days.length; i++) {
days[i] = i*p; days[i] = i * p;
} }
}else if (cfg.matches("^([0-5]??[0-9]??,)+([0-5]??[0-9]??)?$")){//1,3 } else if (cfg.matches("^([0-5]??[0-9]??,)+([0-5]??[0-9]??)?$")) {//1,3
String[] strArr = cfg.split(","); String[] strArr = cfg.split(",");
days = new int[strArr.length]; days = new int[strArr.length];
for (int i = 0; i < strArr.length; i++) { for (int i = 0; i < strArr.length; i++) {
days[i] = Integer.parseInt(strArr[i]); days[i] = Integer.parseInt(strArr[i]);
} }
}else if (cfg.matches("^[0-5]??[0-9]??\\-[0-5]??[0-9]??$")){//1-3 } else if (cfg.matches("^[0-5]??[0-9]??\\-[0-5]??[0-9]??$")) {//1-3
String[] split = cfg.split("-"); String[] split = cfg.split("-");
int s = Integer.parseInt(split[0]); int s = Integer.parseInt(split[0]);
int e = Integer.parseInt(split[1]); int e = Integer.parseInt(split[1]);
days = new int[e-s +1]; days = new int[e - s + 1];
for (int i = 0; i < days.length; i++) { for (int i = 0; i < days.length; i++) {
days[i] = s+i; days[i] = s + i;
} }
}else if (cfg.matches("^[0-5]??[0-9]??\\-[0-5]??[0-9]??/[0-5]??[0-9]??$")){//3-18/5 } else if (cfg.matches("^[0-5]??[0-9]??\\-[0-5]??[0-9]??/[0-5]??[0-9]??$")) {//3-18/5
String[] strArr = cfg.split("/"); String[] strArr = cfg.split("/");
String[] str2Arr = strArr[0].split("-"); String[] str2Arr = strArr[0].split("-");
int s = Integer.parseInt(str2Arr[0]); int s = Integer.parseInt(str2Arr[0]);
int e = Integer.parseInt(str2Arr[1]); int e = Integer.parseInt(str2Arr[1]);
int p = Integer.parseInt(strArr[1]); int p = Integer.parseInt(strArr[1]);
days = new int[(e-s)/p]; days = new int[(e - s) / p];
for (int i = 0; i < days.length; i++) { for (int i = 0; i < days.length; i++) {
days[i] = s + i*p; days[i] = s + i * p;
} }
} }
@ -464,16 +473,16 @@ public class ScheduledExpres implements Scheduled{
//int week = 7 - Math.abs(i - firstWeek) % 7;//当前星期X //int week = 7 - Math.abs(i - firstWeek) % 7;//当前星期X
int week; int week;
int d = days[i]; int d = days[i];
if (d + firstWeek <= 8){ if (d + firstWeek <= 8) {
week = firstWeek + d - 1; week = firstWeek + d - 1;
}else { } else {
week = (d - (8-firstWeek))%7; week = (d - (8 - firstWeek)) % 7;
if (week == 0) week = 7; if (week == 0) week = 7;
} }
//System.out.printf("M:%s,d:%s,w:%s%n", month, d, week); //System.out.printf("M:%s,d:%s,w:%s%n", month, d, week);
if (Arrays.binarySearch(weeks, week) > -1){ if (Arrays.binarySearch(weeks, week) > -1) {
allDay.add(d);//加入日期 allDay.add(d);//加入日期
} }
} }

View File

@ -24,6 +24,7 @@ public interface Task extends Runnable {
/** /**
* 得到下一次执行计划的时间并设置thenTime * 得到下一次执行计划的时间并设置thenTime
*
* @return * @return
*/ */
long nextTime(); long nextTime();
@ -37,18 +38,21 @@ public interface Task extends Runnable {
/** /**
* 是否完成 * 是否完成
*
* @return * @return
*/ */
boolean isComplete(); boolean isComplete();
/** /**
* 完成任务(结束标记) * 完成任务(结束标记)
*
* @param complete * @param complete
*/ */
void setComplete(boolean complete); void setComplete(boolean complete);
/** /**
* 开始时间创建时间 * 开始时间创建时间
*
* @return * @return
*/ */
long startTime(); long startTime();