|
| 1 | +# TaskScheduler |
| 2 | + |
| 3 | +## 简介 |
| 4 | + |
| 5 | +在Android开发场景中,经常会碰到多个耗时任务之间有依赖关系。 |
| 6 | + |
| 7 | +为了快速实现任务可以按照依赖关系调度执行,本库基于Kotlin的协程来实现线程的切换,以及基于图论的DAG(DirectedAcyclicGraph:有向无环图)来实现任务的依赖调度。 |
| 8 | + |
| 9 | +## 主要功能 |
| 10 | + |
| 11 | +### 依赖任务调度 |
| 12 | + |
| 13 | +可以实现异步任务按照依赖关系进行调度执行,常见使用场景: |
| 14 | + |
| 15 | +- 执行任务一开始就已经准备就绪,依赖关系构造后可以直接调度执行,如启动任务依赖执行 |
| 16 | + |
| 17 | +- 执行任务一开始未准备就绪,只能先构造依赖关系,执行任务准备完毕后根据依赖关系来决定是否调度还是继续等待 |
| 18 | + |
| 19 | +任务调度`doOnScheduled`为`suspend`函数对象,执行在主线程,具体任务执行由开发者自行决定执行在什么线程,`doOnScheduled`调度执行结束表示任务结束,将进行后续的调度。 |
| 20 | + |
| 21 | +### 循环任务检查 |
| 22 | + |
| 23 | +当依赖任务之间出现循环依赖时,在构造任务图的时候将抛出异常。 |
| 24 | + |
| 25 | +## 使用方式 |
| 26 | + |
| 27 | +`build.gradle`添加依赖: |
| 28 | + |
| 29 | +```groovy |
| 30 | +// 确保仓库已添加:mavenCentral() |
| 31 | +allprojects { |
| 32 | + repositories { |
| 33 | + ... |
| 34 | + mavenCentral() |
| 35 | + } |
| 36 | +} |
| 37 | +
|
| 38 | +// 添加依赖 |
| 39 | +dependencies { |
| 40 | + compile 'io.github.wurensen:taskscheduler:<version>' |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +> `<version>`请替换为对应的版本号。 |
| 45 | +
|
| 46 | +构造依赖图并执行调度: |
| 47 | + |
| 48 | +```kotlin |
| 49 | +fun test_TaskScheduler() { |
| 50 | + runBlocking { |
| 51 | + val task1 = TaskScheduler.Task("1").apply { |
| 52 | + doOnScheduled = { |
| 53 | + delay(1000) |
| 54 | + log("任务执行完成:$this") |
| 55 | + } |
| 56 | + } |
| 57 | + val task2 = TaskScheduler.Task("2").apply { |
| 58 | + doOnScheduled = { |
| 59 | + delay(1000) |
| 60 | + log("任务执行完成:$this") |
| 61 | + } |
| 62 | + } |
| 63 | + val task3 = TaskScheduler.Task("3").apply { |
| 64 | + doOnScheduled = { |
| 65 | + delay(2000) |
| 66 | + log("任务执行完成:$this") |
| 67 | + } |
| 68 | + } |
| 69 | + val task4 = TaskScheduler.Task("4").apply { |
| 70 | + doOnScheduled = { |
| 71 | + delay(1000) |
| 72 | + log("任务执行完成:$this") |
| 73 | + } |
| 74 | + } |
| 75 | + val task5 = TaskScheduler.Task("5").apply { |
| 76 | + doOnScheduled = { |
| 77 | + delay(1000) |
| 78 | + log("任务执行完成:$this") |
| 79 | + } |
| 80 | + } |
| 81 | + val graph = DirectedAcyclicGraph.Builder<TaskScheduler.Task>() |
| 82 | + .addEdge(task1, task2) |
| 83 | + .addEdge(task1, task3) |
| 84 | + .addEdge(task2, task4) |
| 85 | + .addEdge(task3, task4) |
| 86 | + .addNode(task5) |
| 87 | + .build() |
| 88 | + val taskScheduler = TaskScheduler(MainScope(), graph) |
| 89 | + taskScheduler.schedule(task1) |
| 90 | + .schedule(task2) |
| 91 | + .schedule(task3) |
| 92 | + .schedule(task4) |
| 93 | + .schedule(task5) |
| 94 | + delay(5000) |
| 95 | + } |
| 96 | + |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +更多使用例子,请参考:[测试用例](taskscheduler/src/androidTest/java/com/lancewu/taskscheduler/TaskSchedulerTest.kt) |
| 101 | + |
| 102 | +## Change Log |
| 103 | + |
| 104 | +[Change Log](CHANGELOG.md) |
| 105 | + |
| 106 | +## License |
| 107 | + |
| 108 | +```txt |
| 109 | +Copyright 2022 LanceWu |
| 110 | +
|
| 111 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 112 | +you may not use this file except in compliance with the License. |
| 113 | +You may obtain a copy of the License at |
| 114 | +
|
| 115 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 116 | +
|
| 117 | +Unless required by applicable law or agreed to in writing, software |
| 118 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 119 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 120 | +See the License for the specific language governing permissions and |
| 121 | +limitations under the License. |
| 122 | +``` |
0 commit comments