This commit is contained in:
lxyer 2018-12-07 19:13:55 +08:00
parent 52ade85767
commit 54cff118c3
4 changed files with 27 additions and 5 deletions

View File

@ -15,6 +15,8 @@ public abstract class AbstractTask implements Task {
private boolean isComplete;
private long startTime = System.currentTimeMillis();
private TimerExecutor timerExecutor;
public AbstractTask(String name, Scheduled scheduled) {
this.name = name;
this.scheduled = scheduled;
@ -55,6 +57,15 @@ public abstract class AbstractTask implements Task {
public void setComplete(boolean complete) {
isComplete = complete;
timerExecutor.remove(name);
}
public TimerExecutor getTimerExecutor() {
return timerExecutor;
}
public void setTimerExecutor(TimerExecutor timerExecutor) {
this.timerExecutor = timerExecutor;
}
public long startTime() {

View File

@ -50,4 +50,8 @@ public interface Task extends Runnable{
* @return
*/
long startTime();
TimerExecutor getTimerExecutor();
void setTimerExecutor(TimerExecutor timerExecutor);
}

View File

@ -2,6 +2,7 @@ package com.lxyer.timer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.BiConsumer;
/**
* @author: liangxianyou
@ -17,10 +18,13 @@ public class TimerExecutor {
public void add(Task ... task){
for (Task t : task) {
t.setTimerExecutor(this);
queue.put(t);
}
}
private void add(Task task, boolean upTime){
task.setTimerExecutor(this);
if (upTime) task.nextTime();
queue.put(task);
}

View File

@ -22,19 +22,22 @@ public class TimerTest {
@Test
public void t2() throws InterruptedException {
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", new ScheduledCycle(5000 * 1));
timerExecutor.add(t1, t2);
//timerExecutor.add(t2);
//60s后修改a1 每2s执行一次
Thread.sleep(1000);
//Thread.sleep(1000);
//task = timerExecutor.get("a1");
if (t1 != null){
/*if (t1 != null){
t1.setScheduled(new ScheduledCycle(2000 * 1));
timerExecutor.add(t1);
}
}*/
Thread.sleep(5000);