.
This commit is contained in:
parent
03eabb1b81
commit
03bde29792
@ -9,7 +9,7 @@ redtimer是本人纯手工精心编写的一个任务定时调度器,
|
||||
项目地址:[https://gitee.com/tc608/redtimer](https://gitee.com/tc608/redtimer)
|
||||
|
||||
#### 软件架构
|
||||
里面的东西很简单,没什么架构而言
|
||||

|
||||
|
||||
|
||||
#### 安装使用教程
|
||||
|
@ -19,6 +19,11 @@ public abstract class AbstractTask implements Task {
|
||||
this.theTime = scheduled.theTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long nextTime(){
|
||||
LocalDateTime next = scheduled.nextTime();
|
||||
|
@ -5,6 +5,8 @@ package com.lxyer.timer;
|
||||
*/
|
||||
public interface Task extends Runnable{
|
||||
|
||||
String getName();
|
||||
long nextTime();
|
||||
long theTime();
|
||||
void run();
|
||||
}
|
||||
|
@ -14,10 +14,10 @@ public class TimerExecutor {
|
||||
executor = Executors.newFixedThreadPool(n);
|
||||
}
|
||||
|
||||
public void add(AbstractTask task){
|
||||
public void add(Task task){
|
||||
queue.put(task);
|
||||
}
|
||||
private void add(AbstractTask task, boolean upTime){
|
||||
private void add(Task task, boolean upTime){
|
||||
if (upTime) task.nextTime();
|
||||
queue.put(task);
|
||||
}
|
||||
@ -27,7 +27,7 @@ public class TimerExecutor {
|
||||
new Thread(()->{
|
||||
while (true){
|
||||
try{
|
||||
AbstractTask take = null;
|
||||
Task take = null;
|
||||
try {
|
||||
take = queue.take();
|
||||
} catch (InterruptedException e) {
|
||||
|
@ -9,12 +9,12 @@ import java.util.Set;
|
||||
*/
|
||||
class TimerQueue{
|
||||
Object lock = new Object();
|
||||
AbstractTask[] queue = new AbstractTask[128];
|
||||
Task[] queue = new Task[128];
|
||||
Set<String> names = new HashSet<>();
|
||||
int size=0;
|
||||
|
||||
void put(AbstractTask task) {
|
||||
remove(task.name);
|
||||
void put(Task task) {
|
||||
remove(task.getName());
|
||||
synchronized (lock){
|
||||
int inx = size;//目标坐标
|
||||
while (inx > 0 && queue[inx-1].theTime() > task.theTime()){
|
||||
@ -31,12 +31,12 @@ class TimerQueue{
|
||||
queue[inx] = task;
|
||||
|
||||
size++;
|
||||
names.add(task.name);
|
||||
names.add(task.getName());
|
||||
lock.notify();
|
||||
}
|
||||
}
|
||||
|
||||
AbstractTask take() throws InterruptedException {
|
||||
Task take() throws InterruptedException {
|
||||
synchronized (lock){
|
||||
if (size == 0) lock.wait();
|
||||
|
||||
@ -44,7 +44,7 @@ class TimerQueue{
|
||||
long nextTime = queue[0].theTime();
|
||||
|
||||
if (currentTime >= nextTime){
|
||||
AbstractTask task = queue[0];
|
||||
Task task = queue[0];
|
||||
for (int i = 0; i < size;i++) {
|
||||
queue[i] = queue[i+1];
|
||||
}
|
||||
@ -58,13 +58,13 @@ class TimerQueue{
|
||||
}
|
||||
}
|
||||
|
||||
AbstractTask remove(String name){
|
||||
Task remove(String name){
|
||||
synchronized (lock){
|
||||
if(!names.contains(name)) return null;
|
||||
|
||||
AbstractTask take = null;
|
||||
Task take = null;
|
||||
for (int i = 0; i < size-1; i++) {
|
||||
if (name.equals(queue[i].name)){
|
||||
if (name.equals(queue[i].getName())){
|
||||
take = queue[i];
|
||||
while (i < size+1){
|
||||
queue[i] = queue[i+1];
|
||||
|
Loading…
Reference in New Issue
Block a user