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

View File

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

View File

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