Skip to content

Commit 461ac72

Browse files
committed
Refactor TaskSchedule to support schedule handler
1 parent 624aaab commit 461ac72

File tree

8 files changed

+315
-284
lines changed

8 files changed

+315
-284
lines changed

baseLib/src/androidTest/java/me/ycdev/android/lib/common/async/HandlerExecutorTest.kt renamed to baseLib/src/androidTest/java/me/ycdev/android/lib/common/async/HandlerTaskExecutorTest.kt

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,21 @@ import java.util.concurrent.TimeUnit
1010
import org.junit.Test
1111

1212
@LargeTest
13-
class HandlerExecutorTest {
13+
class HandlerTaskExecutorTest {
1414
@Test
1515
@Throws(InterruptedException::class)
1616
fun checkLooper() {
1717
println("test thread id=" + Thread.currentThread().id)
18-
val executor = HandlerExecutor(Looper.getMainLooper())
18+
val executor = HandlerTaskExecutor.withMainLooper()
1919
val latch = CountDownLatch(4)
20-
executor.postTasks(createTasks(latch, 4, 150))
20+
createTasks(latch, 4, 150).forEach {
21+
executor.postTask(it)
22+
}
2123
latch.await(1, TimeUnit.SECONDS)
2224
assertThat(latch.count).isEqualTo(0L)
2325
}
2426

25-
@Test
26-
@Throws(InterruptedException::class)
27-
fun clearTasks() {
28-
val executor = HandlerExecutor(Looper.getMainLooper())
29-
val latch = CountDownLatch(5)
30-
executor.postTasks(createTasks(latch, 5, 100))
31-
SystemClock.sleep(250)
32-
executor.clearTasks()
33-
latch.await(1, TimeUnit.SECONDS)
34-
assertThat(latch.count).isGreaterThan(1L)
35-
assertThat(latch.count).isLessThan(5L)
36-
}
37-
27+
@Suppress("SameParameterValue")
3828
private fun createTasks(latch: CountDownLatch, count: Int, sleepMs: Long): List<Runnable> {
3929
val tasks = ArrayList<Runnable>(count)
4030
for (i in 0 until count) {

baseLib/src/androidTest/java/me/ycdev/android/lib/common/async/TaskSchedulerTest.kt

Lines changed: 146 additions & 101 deletions
Large diffs are not rendered by default.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package me.ycdev.android.lib.common.annotation
2+
3+
/**
4+
* Denotes that the annotated method can only be executed in the specified handler.
5+
*/
6+
@Target(
7+
AnnotationTarget.FUNCTION,
8+
AnnotationTarget.PROPERTY_GETTER,
9+
AnnotationTarget.PROPERTY_SETTER
10+
)
11+
@Retention(AnnotationRetention.SOURCE)
12+
annotation class HandlerWork(val value: String)

baseLib/src/main/java/me/ycdev/android/lib/common/async/HandlerExecutor.kt

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package me.ycdev.android.lib.common.async
2+
3+
import android.os.Handler
4+
import android.os.HandlerThread
5+
import android.os.Looper
6+
import androidx.annotation.NonNull
7+
8+
open class HandlerTaskExecutor(@NonNull val taskHandler: Handler) : ITaskExecutor {
9+
10+
override fun postTask(task: Runnable) {
11+
taskHandler.post(task)
12+
}
13+
14+
companion object {
15+
fun withMainLooper(): HandlerTaskExecutor {
16+
return HandlerTaskExecutor(Handler(Looper.getMainLooper()))
17+
}
18+
19+
fun withHandlerThread(name: String): HandlerTaskExecutor {
20+
val thread = HandlerThread(name)
21+
thread.start()
22+
return HandlerTaskExecutor(Handler(thread.looper))
23+
}
24+
}
25+
}

baseLib/src/main/java/me/ycdev/android/lib/common/async/ITaskExecutor.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@ interface ITaskExecutor {
44
/**
55
* Post a task to execute.
66
*
7-
*
87
* This method should return immediately and the task should be executed asynchronously.
98
*/
10-
fun postTasks(tasks: List<Runnable>)
11-
12-
/**
13-
* Clear all pending tasks.
14-
*/
15-
fun clearTasks()
9+
fun postTask(task: Runnable)
1610
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package me.ycdev.android.lib.common.async
2+
3+
import android.os.SystemClock
4+
import me.ycdev.android.lib.common.utils.DateTimeUtils
5+
import java.lang.StringBuilder
6+
import java.util.concurrent.atomic.AtomicInteger
7+
8+
internal class TaskInfo(val executor: ITaskExecutor, val task: Runnable, val delay: Long, val period: Long = -1) {
9+
private val taskId: Int = taskIdGenerator.incrementAndGet()
10+
var triggerAt: Long = SystemClock.elapsedRealtime() + delay
11+
12+
override fun toString(): String {
13+
val timestamp = System.currentTimeMillis() - (SystemClock.elapsedRealtime() - triggerAt)
14+
return StringBuilder().append("TaskInfo[id=").append(taskId)
15+
.append(", delay=").append(delay)
16+
.append(", triggerAt=").append(DateTimeUtils.getReadableTimeStamp(timestamp))
17+
.append(", period=").append(period)
18+
.append(']')
19+
.toString()
20+
}
21+
22+
companion object {
23+
private val taskIdGenerator = AtomicInteger(0)
24+
}
25+
}

0 commit comments

Comments
 (0)