├── README.md
├── .gitignore
├── pom.xml
└── src
└── main
└── java
└── tech
└── insight
├── Main.java
├── Job.java
└── ScheduleService.java
/README.md:
--------------------------------------------------------------------------------
1 | ## 一个简单的定时任务实现
2 |
3 | B站视频链接 : [跳转B站](https://www.bilibili.com/video/BV19jffYVE3C/?vd_source=b13a31d63cf084a4bc7e9d71d9c78835#reply113889094272315)
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 | !**/src/main/**/target/
4 | !**/src/test/**/target/
5 |
6 | ### IntelliJ IDEA ###
7 | .idea/*
8 | *.iws
9 | *.iml
10 | *.ipr
11 |
12 | ### Eclipse ###
13 | .apt_generated
14 | .classpath
15 | .factorypath
16 | .project
17 | .settings
18 | .springBeans
19 | .sts4-cache
20 |
21 | ### NetBeans ###
22 | /nbproject/private/
23 | /nbbuild/
24 | /dist/
25 | /nbdist/
26 | /.nb-gradle/
27 | build/
28 | !**/src/main/**/build/
29 | !**/src/test/**/build/
30 |
31 | ### VS Code ###
32 | .vscode/
33 |
34 | ### Mac OS ###
35 | .DS_Store
36 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | tech.insight
8 | mini_schedule
9 | 1.0-SNAPSHOT
10 |
11 |
12 | 17
13 | 17
14 | UTF-8
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/src/main/java/tech/insight/Main.java:
--------------------------------------------------------------------------------
1 | package tech.insight;
2 |
3 |
4 | import java.time.LocalDateTime;
5 | import java.time.format.DateTimeFormatter;
6 |
7 | /**
8 | * @author gongxuanzhangmelt@gmail.com
9 | **/
10 | public class Main {
11 | public static void main(String[] args) throws InterruptedException {
12 | DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss SSS");
13 | ScheduleService scheduleService = new ScheduleService();
14 | scheduleService.schedule(()->{
15 | System.out.println(LocalDateTime.now().format(dateTimeFormatter) +" 这是 100毫秒一次的");
16 | },100);
17 |
18 | Thread.sleep(1000);
19 | System.out.println("添加一个每200毫秒打印一个2的定时任务");
20 | scheduleService.schedule(()->{
21 | System.out.println(LocalDateTime.now().format(dateTimeFormatter) +" 这是 200毫秒一次的");
22 | },200);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/tech/insight/Job.java:
--------------------------------------------------------------------------------
1 | package tech.insight;
2 |
3 | /**
4 | * @author gongxuanzhangmelt@gmail.com
5 | **/
6 | public class Job implements Comparable {
7 |
8 | private Runnable task;
9 |
10 | private long startTime;
11 |
12 | private long delay;
13 |
14 | public long getDelay() {
15 | return delay;
16 | }
17 |
18 | public Job setDelay(long delay) {
19 | this.delay = delay;
20 | return this;
21 | }
22 |
23 | public Runnable getTask() {
24 | return task;
25 | }
26 |
27 | public Job setTask(Runnable task) {
28 | this.task = task;
29 | return this;
30 | }
31 |
32 | public long getStartTime() {
33 | return startTime;
34 | }
35 |
36 | public Job setStartTime(long startTime) {
37 | this.startTime = startTime;
38 | return this;
39 | }
40 |
41 | @Override
42 | public int compareTo(Job o) {
43 | return Long.compare(this.startTime, o.startTime);
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/tech/insight/ScheduleService.java:
--------------------------------------------------------------------------------
1 | package tech.insight;
2 |
3 |
4 | import java.util.concurrent.ExecutorService;
5 | import java.util.concurrent.Executors;
6 | import java.util.concurrent.PriorityBlockingQueue;
7 | import java.util.concurrent.locks.LockSupport;
8 |
9 | /**
10 | * @author gongxuanzhangmelt@gmail.com
11 | **/
12 | public class ScheduleService {
13 |
14 |
15 | Trigger trigger = new Trigger();
16 |
17 | ExecutorService executorService = Executors.newFixedThreadPool(6);
18 |
19 | void schedule(Runnable task, long delay) {
20 | Job job = new Job();
21 | job.setTask(task);
22 | job.setStartTime(System.currentTimeMillis() + delay);
23 | job.setDelay(delay);
24 | trigger.queue.offer(job);
25 | trigger.wakeUp();
26 | }
27 |
28 | // 等待合适的时间,把对应的任务扔到线程池中
29 | class Trigger {
30 |
31 | PriorityBlockingQueue queue = new PriorityBlockingQueue<>();
32 |
33 | Thread thread = new Thread(() -> {
34 | while (true) {
35 | while (queue.isEmpty()) {
36 | LockSupport.park();
37 | }
38 | Job latelyJob = queue.peek();
39 | if (latelyJob.getStartTime() < System.currentTimeMillis()) {
40 | latelyJob = queue.poll();
41 | executorService.execute(latelyJob.getTask());
42 | Job nextJob = new Job();
43 | nextJob.setTask(latelyJob.getTask());
44 | nextJob.setDelay(latelyJob.getDelay());
45 | nextJob.setStartTime(System.currentTimeMillis() + latelyJob.getDelay());
46 | queue.offer(nextJob);
47 | } else {
48 | LockSupport.parkUntil(latelyJob.getStartTime());
49 | }
50 | }
51 | });
52 |
53 | {
54 | thread.start();
55 | System.out.println("触发器启动了!");
56 | }
57 |
58 | void wakeUp() {
59 | LockSupport.unpark(thread);
60 | }
61 | }
62 |
63 |
64 | }
65 |
--------------------------------------------------------------------------------