This commit is contained in:
lxyer 2018-12-10 22:16:23 +08:00
parent c3e2a47b7e
commit 3d5387615d
4 changed files with 21 additions and 25 deletions

View File

@ -64,7 +64,7 @@ public class TimerTask implements Task {
} }
public void setComplete(boolean complete) { public void setComplete(boolean complete) {
isComplete = complete; if (isComplete = complete)
timerExecutor.remove(name); timerExecutor.remove(name);
} }
@ -82,20 +82,16 @@ public class TimerTask implements Task {
@Override @Override
public void run() { public void run() {
//没有完成任务继续执行返回true,表示完成 //没有完成任务继续执行
if (!isComplete) { if (!isComplete) {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
StringBuilder buf = new StringBuilder(); job.execute(this);
buf.append("task [" + getName() + "] : ").append("not complete -> "); long end = System.currentTimeMillis();
long end; logger.log(Level.INFO, String.format("task [%s] : not complete -> %s, time: %s ms", getName(), isComplete ? "had complete" : "not complete;", end - start));
if (!(isComplete = job.execute())) {
end = System.currentTimeMillis();
timerExecutor.add(this, true);
} else {
end = System.currentTimeMillis();
}
logger.log(Level.INFO, buf.append(isComplete ? "had complete" : "not complete;").append("time: ").append(end - start).append(" ms").toString()); if (!isComplete) {
timerExecutor.add(this, true);
}
} }
} }

View File

@ -8,12 +8,7 @@ public interface Job {
/** /**
* 任务执行的内容 * 任务执行的内容
* @return true:完成完成任务false未完成任务
*/ */
boolean execute(); void execute(Task task);
/*default Job then(Job job) {
return job;
}*/
} }

View File

@ -41,6 +41,12 @@ public interface Task extends Runnable {
*/ */
boolean isComplete(); boolean isComplete();
/**
* 完成任务(结束标记)
* @param complete
*/
void setComplete(boolean complete);
/** /**
* 开始时间创建时间 * 开始时间创建时间
* @return * @return

View File

@ -24,10 +24,9 @@ public class TimerTest {
TimerExecutor timerExecutor = new TimerExecutor(1); TimerExecutor timerExecutor = new TimerExecutor(1);
//Task t1 = new TaskImpl("a1", new ScheduledExpres("1-40 * * * *"));//1-40定时每分钟执行 //Task t1 = new TaskImpl("a1", new ScheduledExpres("1-40 * * * *"));//1-40定时每分钟执行
//TaskImpl t2 = new TaskImpl("a2", ScheduledCycle.of(5000 * 1)); //TaskImpl t2 = new TaskImpl("a2", ScheduledCycle.of(5000 * 1));
Task task = TimerTask.by("A2", ScheduledCycle.of(1000 * 1), () -> { Task task = TimerTask.by("A2", ScheduledCycle.of(1000 * 1), (t) -> {
System.out.println("xxxx"); System.out.println("xxxx");
return false;// false 继续执行 true结束任务
}); });
@ -149,7 +148,7 @@ public class TimerTest {
08 18 * 7,8 4 08 18 * 7,8 4
"task1", "1 22-23 * * 7" "task1", "1 22-23 * * 7"
*/ */
Task task = TimerTask.by("task1", ScheduledExpres.of("1 22-23 * * 7"), () -> { Task task = TimerTask.by("task1", ScheduledExpres.of("1 22-23 * * 7"), (t) -> {
//System.out.println(""); //System.out.println("");
@ -169,7 +168,6 @@ public class TimerTest {
System.out.println(new SimpleDateFormat("12: yyyy-MM-dd HH:mm:ss").format(nextTime())); System.out.println(new SimpleDateFormat("12: yyyy-MM-dd HH:mm:ss").format(nextTime()));
System.out.println(new SimpleDateFormat("13: yyyy-MM-dd HH:mm:ss").format(nextTime()));*/ System.out.println(new SimpleDateFormat("13: yyyy-MM-dd HH:mm:ss").format(nextTime()));*/
return true;
}); });
task.run(); task.run();
task.setScheduled(ScheduledCycle.of(1000 * 5));//定时每秒执行 task.setScheduled(ScheduledCycle.of(1000 * 5));//定时每秒执行
@ -181,14 +179,15 @@ public class TimerTest {
public void t7() { public void t7() {
TimerExecutor executor = new TimerExecutor(1); TimerExecutor executor = new TimerExecutor(1);
executor.add(TimerTask.by("A1", ScheduledCycle.of(1000 * 5), () -> { executor.add(TimerTask.by("A1", ScheduledCycle.of(1000 * 5), (t) -> {
try { try {
Thread.sleep(6); Thread.sleep(6);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
System.out.println("task do.."); System.out.println("task do..");
return true;
t.setComplete(true);//通过标记控制任务结束
})); }));