格式调整
This commit is contained in:
parent
2a085daa2a
commit
080c6d22ee
@ -20,7 +20,7 @@ public class TimerExecutor {
|
||||
start();
|
||||
}
|
||||
|
||||
public void add(Task... task){
|
||||
public void add(Task... task) {
|
||||
for (Task t : task) {
|
||||
t.setTimerExecutor(this);
|
||||
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);
|
||||
if (upTime) task.nextTime();
|
||||
queue.push(task);
|
||||
}
|
||||
|
||||
public Task remove(String name){
|
||||
public Task remove(String name) {
|
||||
return queue.remove(name);
|
||||
}
|
||||
public Task get(String name){
|
||||
|
||||
public Task get(String name) {
|
||||
return queue.get(name);
|
||||
}
|
||||
|
||||
|
||||
public void start() {
|
||||
new Thread(()->{
|
||||
while (true){
|
||||
try{
|
||||
new Thread(() -> {
|
||||
while (true) {
|
||||
try {
|
||||
Task take = null;
|
||||
try {
|
||||
take = queue.take();
|
||||
@ -56,7 +57,7 @@ public class TimerExecutor {
|
||||
//执行调度
|
||||
executor.execute(take);
|
||||
//add(take, true); //继续添加任务到 队列
|
||||
}catch (Exception e){
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ import net.tccn.timer.task.Task;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
@ -44,7 +43,7 @@ public class TimerTask implements Task {
|
||||
}
|
||||
|
||||
@Override
|
||||
public long nextTime(){
|
||||
public long nextTime() {
|
||||
LocalDateTime next = scheduled.nextTime();
|
||||
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()));*/
|
||||
return theTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long theTime(){
|
||||
public long theTime() {
|
||||
LocalDateTime next = scheduled.theTime();
|
||||
this.theTime = Date.from(next.atZone(ZoneId.systemDefault()).toInstant()).getTime();
|
||||
return theTime;
|
||||
|
@ -9,29 +9,30 @@ import java.util.Set;
|
||||
/**
|
||||
* Created by liangxianyou at 2018/7/23 14:07.
|
||||
*/
|
||||
public class TimerQueue{
|
||||
public class TimerQueue {
|
||||
Object lock = new Object();
|
||||
Task[] queue = new Task[128];
|
||||
Set<String> names = new HashSet<>();
|
||||
int size=0;
|
||||
int size = 0;
|
||||
|
||||
/**
|
||||
* 新加调度任务
|
||||
*
|
||||
* @param task
|
||||
*/
|
||||
public void push(Task task) {
|
||||
synchronized (lock){
|
||||
synchronized (lock) {
|
||||
remove(task.getName());
|
||||
int inx = size;//目标坐标
|
||||
while (inx > 0 && queue[inx-1].theTime() > task.theTime()){
|
||||
while (inx > 0 && queue[inx - 1].theTime() > task.theTime()) {
|
||||
inx--;
|
||||
}
|
||||
|
||||
if (queue.length == size+1)
|
||||
if (queue.length == size + 1)
|
||||
queue = Arrays.copyOf(queue, size * 2);
|
||||
|
||||
for (int i = size+1; i > inx; i--) {
|
||||
queue[i] = queue[i-1];
|
||||
for (int i = size + 1; i > inx; i--) {
|
||||
queue[i] = queue[i - 1];
|
||||
}
|
||||
queue[inx] = task;
|
||||
|
||||
@ -43,25 +44,26 @@ public class TimerQueue{
|
||||
|
||||
/**
|
||||
* 调度等待执行的任务
|
||||
*
|
||||
* @return
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public Task take() throws InterruptedException {
|
||||
synchronized (lock){
|
||||
synchronized (lock) {
|
||||
while (size == 0) lock.wait(10);//循环避免非put线程唤醒空异常
|
||||
|
||||
long currentTime = System.currentTimeMillis();
|
||||
long nextTime = queue[0].theTime();
|
||||
|
||||
if (currentTime >= nextTime){
|
||||
if (currentTime >= nextTime) {
|
||||
Task task = queue[0];
|
||||
for (int i = 0; i < size;i++) {
|
||||
queue[i] = queue[i+1];
|
||||
for (int i = 0; i < size; i++) {
|
||||
queue[i] = queue[i + 1];
|
||||
}
|
||||
queue[size-1] = null;
|
||||
queue[size - 1] = null;
|
||||
size--;
|
||||
return task;
|
||||
}else {
|
||||
} else {
|
||||
lock.wait(nextTime - currentTime);
|
||||
return take();
|
||||
}
|
||||
@ -70,34 +72,36 @@ public class TimerQueue{
|
||||
|
||||
/**
|
||||
* 删除指定名称的任务
|
||||
*
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
public Task remove(String name){
|
||||
public Task remove(String name) {
|
||||
return get(name, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回指定名称的任务
|
||||
*
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
public Task get(String name){
|
||||
public Task get(String name) {
|
||||
return get(name, false);
|
||||
}
|
||||
|
||||
private Task get(String name, boolean remove) {
|
||||
synchronized (lock){
|
||||
if(!names.contains(name)) return null;
|
||||
synchronized (lock) {
|
||||
if (!names.contains(name)) return null;
|
||||
|
||||
Task take = null;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (name.equals(queue[i].getName())){
|
||||
if (name.equals(queue[i].getName())) {
|
||||
take = queue[i];
|
||||
if (!remove) break;
|
||||
while (i < size+1){
|
||||
queue[i] = queue[i+1];
|
||||
queue[i+1] = null;
|
||||
while (i < size + 1) {
|
||||
queue[i] = queue[i + 1];
|
||||
queue[i + 1] = null;
|
||||
i++;
|
||||
}
|
||||
names.remove(name);
|
||||
|
@ -9,12 +9,14 @@ public interface Scheduled {
|
||||
|
||||
/**
|
||||
* 下次执行时间
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
LocalDateTime nextTime();
|
||||
|
||||
/**
|
||||
* 当前执行时间
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
LocalDateTime theTime();
|
||||
|
@ -8,10 +8,11 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* 时间解析器
|
||||
*
|
||||
* @author: liangxianyou
|
||||
*/
|
||||
@SuppressWarnings("Duplicates")
|
||||
public class ScheduledExpres implements Scheduled{
|
||||
public class ScheduledExpres implements Scheduled {
|
||||
private int year;
|
||||
private int month;
|
||||
private int[] minutes;
|
||||
@ -23,24 +24,27 @@ public class ScheduledExpres implements Scheduled{
|
||||
private String cfg;
|
||||
private String[] cfgArr;
|
||||
private LocalDateTime theTime;
|
||||
private int _y,_M,_d,_H,_m;
|
||||
private int _y, _M, _d, _H, _m;
|
||||
|
||||
@Deprecated
|
||||
private ScheduledExpres(String cfg){
|
||||
private ScheduledExpres(String cfg) {
|
||||
this.cfg = cfg;
|
||||
this.theTime = LocalDateTime.now();
|
||||
initTheTime();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
private ScheduledExpres(final LocalDateTime startTime, String cfg){
|
||||
private ScheduledExpres(final LocalDateTime startTime, String cfg) {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
this.theTime = now.isAfter(startTime)? now : startTime;
|
||||
this.theTime = now.isAfter(startTime) ? now : startTime;
|
||||
this.cfg = cfg;
|
||||
initTheTime();
|
||||
}
|
||||
|
||||
public static Scheduled of(String cfg) {
|
||||
return new ScheduledExpres(cfg);
|
||||
}
|
||||
|
||||
public static Scheduled of(final LocalDateTime startTime, String cfg) {
|
||||
return new ScheduledExpres(startTime, cfg);
|
||||
}
|
||||
@ -58,55 +62,56 @@ public class ScheduledExpres implements Scheduled{
|
||||
setMinutes();
|
||||
|
||||
_y = theTime.getYear();
|
||||
_M= theTime.getMonthValue();
|
||||
_d= theTime.getDayOfMonth();
|
||||
_H= theTime.getHour();
|
||||
_m= theTime.getMinute();
|
||||
_M = theTime.getMonthValue();
|
||||
_d = theTime.getDayOfMonth();
|
||||
_H = theTime.getHour();
|
||||
_m = theTime.getMinute();
|
||||
|
||||
String cmd = "";//y M d H m
|
||||
if (days.length == 0) cmd = "M";
|
||||
do {
|
||||
carry(cmd);
|
||||
int inx;
|
||||
if ((inx = nowOk(monthes, _M)) < 0){
|
||||
if ((inx = nowOk(monthes, _M)) < 0) {
|
||||
cmd = "y";
|
||||
continue;
|
||||
}
|
||||
_M = monthes[inx];
|
||||
|
||||
if ((inx = nowOk(days, _d)) < 0){
|
||||
if ((inx = nowOk(days, _d)) < 0) {
|
||||
cmd = "M";
|
||||
continue;
|
||||
}
|
||||
_d = days[inx];
|
||||
|
||||
if ((inx = nowOk(hours, _H)) < 0){
|
||||
if ((inx = nowOk(hours, _H)) < 0) {
|
||||
cmd = "d";
|
||||
continue;
|
||||
}
|
||||
_H = hours[inx];
|
||||
|
||||
if ((inx = nowOk(minutes, _m)) < 0){
|
||||
if ((inx = nowOk(minutes, _m)) < 0) {
|
||||
cmd = "H";
|
||||
continue;
|
||||
}
|
||||
_m = minutes[inx];
|
||||
break;
|
||||
}while (true);
|
||||
} while (true);
|
||||
|
||||
theTime = LocalDateTime.of(_y, _M, _d, _H, _m);
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
while (theTime.isBefore(now)){
|
||||
while (theTime.isBefore(now)) {
|
||||
theTime = carry("m");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 下一次执行的时间
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public LocalDateTime nextTime(){
|
||||
public LocalDateTime nextTime() {
|
||||
return theTime = carry("m");
|
||||
}
|
||||
|
||||
@ -117,17 +122,18 @@ public class ScheduledExpres implements Scheduled{
|
||||
|
||||
/**
|
||||
* 通过发送指令进行进位
|
||||
*
|
||||
* @param cmd 进位指令
|
||||
*/
|
||||
private LocalDateTime carry(String cmd){
|
||||
private LocalDateTime carry(String cmd) {
|
||||
int inx;
|
||||
while (!"".equals(cmd)){
|
||||
while (!"".equals(cmd)) {
|
||||
switch (cmd) {
|
||||
case "y":
|
||||
_y = this.year = ++_y;
|
||||
_M = this.month = monthes[0];
|
||||
setDays();
|
||||
if (days.length == 0){
|
||||
if (days.length == 0) {
|
||||
cmd = "M";
|
||||
continue;
|
||||
}
|
||||
@ -136,18 +142,18 @@ public class ScheduledExpres implements Scheduled{
|
||||
_m = minutes[0];
|
||||
break;
|
||||
case "M":
|
||||
if (_M < monthes[0]){
|
||||
if (_M < monthes[0]) {
|
||||
_M = monthes[0];
|
||||
break;
|
||||
}
|
||||
inx = Arrays.binarySearch(monthes, _M);
|
||||
if (inx < 0 || inx >= monthes.length-1) {
|
||||
if (inx < 0 || inx >= monthes.length - 1) {
|
||||
cmd = "y";
|
||||
continue ;
|
||||
continue;
|
||||
}
|
||||
_M = this.month = monthes[inx+1];
|
||||
_M = this.month = monthes[inx + 1];
|
||||
setDays();
|
||||
if (days.length == 0){
|
||||
if (days.length == 0) {
|
||||
cmd = "M";
|
||||
continue;
|
||||
}
|
||||
@ -156,43 +162,43 @@ public class ScheduledExpres implements Scheduled{
|
||||
_m = minutes[0];
|
||||
break;
|
||||
case "d":
|
||||
if (_d < days[0]){
|
||||
if (_d < days[0]) {
|
||||
_d = days[0];
|
||||
break;
|
||||
}
|
||||
inx = Arrays.binarySearch(days, _d);
|
||||
if (inx < 0 || inx >= days.length-1) {
|
||||
if (inx < 0 || inx >= days.length - 1) {
|
||||
cmd = "M";
|
||||
continue ;
|
||||
continue;
|
||||
}
|
||||
_d = days[inx+1];
|
||||
_d = days[inx + 1];
|
||||
_H = hours[0];
|
||||
_m = minutes[0];
|
||||
break;
|
||||
case "H":
|
||||
if (_H < hours[0]){
|
||||
if (_H < hours[0]) {
|
||||
_H = hours[0];
|
||||
break;
|
||||
}
|
||||
inx = Arrays.binarySearch(hours, _H);
|
||||
if (inx < 0 || inx >= hours.length -1) {
|
||||
if (inx < 0 || inx >= hours.length - 1) {
|
||||
cmd = "d";
|
||||
continue ;
|
||||
continue;
|
||||
}
|
||||
_H = hours[inx+1];
|
||||
_H = hours[inx + 1];
|
||||
_m = minutes[0];
|
||||
break;
|
||||
case "m":
|
||||
if (_m < minutes[0]){
|
||||
if (_m < minutes[0]) {
|
||||
_m = minutes[0];
|
||||
break;
|
||||
}
|
||||
inx = Arrays.binarySearch(minutes, _m);
|
||||
if (inx < 0 || inx >= minutes.length -1) {
|
||||
if (inx < 0 || inx >= minutes.length - 1) {
|
||||
cmd = "H";
|
||||
continue ;
|
||||
continue;
|
||||
}
|
||||
_m = minutes[inx+1];
|
||||
_m = minutes[inx + 1];
|
||||
break;
|
||||
}
|
||||
cmd = "";
|
||||
@ -202,21 +208,22 @@ public class ScheduledExpres implements Scheduled{
|
||||
|
||||
/**
|
||||
* 得到初始合法时间的索引
|
||||
*
|
||||
* @param arr 合法时间序列
|
||||
* @param n 初始选中值
|
||||
* @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[0] > n)
|
||||
return 0;
|
||||
if (arr[arr.length-1] < n)
|
||||
if (arr[arr.length - 1] < n)
|
||||
return 0;
|
||||
if (arr[arr.length-1] == n)
|
||||
return arr.length-1;
|
||||
if (arr[arr.length - 1] == n)
|
||||
return arr.length - 1;
|
||||
|
||||
for (int i = 0; i < arr.length-1; i++) {
|
||||
if ((arr[i] < n && arr[i+1] > n) || arr[i] == n){
|
||||
for (int i = 0; i < arr.length - 1; i++) {
|
||||
if ((arr[i] < n && arr[i + 1] > n) || arr[i] == n) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
@ -224,237 +231,239 @@ public class ScheduledExpres implements Scheduled{
|
||||
return -1;
|
||||
}
|
||||
|
||||
/** 以下为 初始化合法时间 weeks,monthes,days,hour,minutes 序列 */
|
||||
private void setMinutes(){
|
||||
/**
|
||||
* 以下为 初始化合法时间 weeks,monthes,days,hour,minutes 序列
|
||||
*/
|
||||
private void setMinutes() {
|
||||
String cfg = cfgArr[0];
|
||||
if ("*".equals(cfg)){//*
|
||||
if ("*".equals(cfg)) {//*
|
||||
minutes = new int[60];
|
||||
for (int i = 0; i < 60; 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[0] = Integer.parseInt(cfg);
|
||||
}else if (cfg.matches("^[*]/[0-9]+$")){// */5
|
||||
} else if (cfg.matches("^[*]/[0-9]+$")) {// */5
|
||||
String[] strArr = cfg.split("/");
|
||||
int p = Integer.parseInt(strArr[1]);
|
||||
minutes = new int[60/p];
|
||||
minutes = new int[60 / p];
|
||||
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(",");
|
||||
minutes = new int[strings.length];
|
||||
for (int i = 0; i < strings.length; 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("-");
|
||||
int s = Integer.parseInt(split[0]);
|
||||
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++) {
|
||||
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[] str2Arr = strArr[0].split("-");
|
||||
int s = Integer.parseInt(str2Arr[0]);
|
||||
int e = Integer.parseInt(str2Arr[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++) {
|
||||
minutes[i] = s + i*p;
|
||||
minutes[i] = s + i * p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setHours(){
|
||||
private void setHours() {
|
||||
String cfg = cfgArr[1];
|
||||
if ("*".equals(cfg)){//*
|
||||
if ("*".equals(cfg)) {//*
|
||||
hours = new int[24];
|
||||
for (int i = 0; i < hours.length; 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[0] = Integer.parseInt(cfg);
|
||||
}else if (cfg.matches("^[*]/[0-9]+$")){// */5
|
||||
} else if (cfg.matches("^[*]/[0-9]+$")) {// */5
|
||||
String[] strArr = cfg.split("/");
|
||||
int p = Integer.parseInt(strArr[1]);
|
||||
hours = new int[24/p];
|
||||
hours = new int[24 / p];
|
||||
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(",");
|
||||
hours = new int[strArr.length];
|
||||
for (int i = 0; i < strArr.length; 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("-");
|
||||
int s = Integer.parseInt(split[0]);
|
||||
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++) {
|
||||
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[] str2Arr = strArr[0].split("-");
|
||||
int s = Integer.parseInt(str2Arr[0]);
|
||||
int e = Integer.parseInt(str2Arr[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++) {
|
||||
hours[i] = s + i*p;
|
||||
hours[i] = s + i * p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setWeeks(){
|
||||
private void setWeeks() {
|
||||
String cfg = cfgArr[4];
|
||||
if ("*".equals(cfg)){//*
|
||||
if ("*".equals(cfg)) {//*
|
||||
weeks = new int[7];
|
||||
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[0] = Integer.parseInt(cfg);
|
||||
}else if (cfg.matches("^[*]/[0-9]+$")){// */5
|
||||
} else if (cfg.matches("^[*]/[0-9]+$")) {// */5
|
||||
String[] strArr = cfg.split("/");
|
||||
int p = Integer.parseInt(strArr[1]);
|
||||
weeks = new int[7/p];
|
||||
weeks = new int[7 / p];
|
||||
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(",");
|
||||
weeks = new int[strArr.length];
|
||||
for (int i = 0; i < strArr.length; 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("-");
|
||||
int s = Integer.parseInt(split[0]);
|
||||
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++) {
|
||||
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[] str2Arr = strArr[0].split("-");
|
||||
int s = Integer.parseInt(str2Arr[0]);
|
||||
int e = Integer.parseInt(str2Arr[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++) {
|
||||
weeks[i] = s + i*p;
|
||||
weeks[i] = s + i * p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setMonthes(){
|
||||
private void setMonthes() {
|
||||
String cfg = cfgArr[3];
|
||||
if ("*".equals(cfg)){//*
|
||||
if ("*".equals(cfg)) {//*
|
||||
monthes = new int[12];
|
||||
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[0] = Integer.parseInt(cfg);
|
||||
}else if (cfg.matches("^[*]/[0-9]+$")){// */5
|
||||
} else if (cfg.matches("^[*]/[0-9]+$")) {// */5
|
||||
String[] strArr = cfg.split("/");
|
||||
int p = Integer.parseInt(strArr[1]);
|
||||
monthes = new int[12/p];
|
||||
monthes = new int[12 / p];
|
||||
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(",");
|
||||
monthes = new int[strArr.length];
|
||||
for (int i = 0; i < strArr.length; 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("-");
|
||||
int s = Integer.parseInt(split[0]);
|
||||
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++) {
|
||||
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[] str2Arr = strArr[0].split("-");
|
||||
int s = Integer.parseInt(str2Arr[0]);
|
||||
int e = Integer.parseInt(str2Arr[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++) {
|
||||
monthes[i] = s + i*p;
|
||||
monthes[i] = s + i * p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setDays(){
|
||||
private void setDays() {
|
||||
String cfg = cfgArr[2];
|
||||
//当前月份总天数,
|
||||
LocalDate firstDay = LocalDate.of(year, month, 1);
|
||||
int lengthOfMonth = firstDay.lengthOfMonth();
|
||||
|
||||
if ("*".equals(cfg)){//*
|
||||
if ("*".equals(cfg)) {//*
|
||||
days = new int[lengthOfMonth];
|
||||
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[0] = Integer.parseInt(cfg);
|
||||
}else if (cfg.matches("^[*]/[0-9]+$")){// */5
|
||||
} else if (cfg.matches("^[*]/[0-9]+$")) {// */5
|
||||
String[] strArr = cfg.split("/");
|
||||
int p = Integer.parseInt(strArr[1]);
|
||||
days = new int[lengthOfMonth/p];
|
||||
days = new int[lengthOfMonth / p];
|
||||
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(",");
|
||||
days = new int[strArr.length];
|
||||
for (int i = 0; i < strArr.length; 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("-");
|
||||
int s = Integer.parseInt(split[0]);
|
||||
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++) {
|
||||
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[] str2Arr = strArr[0].split("-");
|
||||
int s = Integer.parseInt(str2Arr[0]);
|
||||
int e = Integer.parseInt(str2Arr[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++) {
|
||||
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;
|
||||
int d = days[i];
|
||||
if (d + firstWeek <= 8){
|
||||
if (d + firstWeek <= 8) {
|
||||
week = firstWeek + d - 1;
|
||||
}else {
|
||||
week = (d - (8-firstWeek))%7;
|
||||
} else {
|
||||
week = (d - (8 - firstWeek)) % 7;
|
||||
if (week == 0) week = 7;
|
||||
}
|
||||
|
||||
//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);//加入日期
|
||||
}
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ public interface Task extends Runnable {
|
||||
|
||||
/**
|
||||
* 得到下一次执行计划的时间,并设置thenTime
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
long nextTime();
|
||||
@ -37,18 +38,21 @@ public interface Task extends Runnable {
|
||||
|
||||
/**
|
||||
* 是否完成
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
boolean isComplete();
|
||||
|
||||
/**
|
||||
* 完成任务(结束标记)
|
||||
*
|
||||
* @param complete
|
||||
*/
|
||||
void setComplete(boolean complete);
|
||||
|
||||
/**
|
||||
* 开始时间(创建时间)
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
long startTime();
|
||||
|
Loading…
Reference in New Issue
Block a user