赞
踩
由于项目中有处理大量后台任务并且串行
执行的需求,特意写了一个简易的任务调度器,方便监控每个任务执行和异常情况,任务之间互不影响。正如上所述,Kotlin中的TaskScheduler类提供了一个强大的解决方案,用于使用ScheduledExecutorService异步地排队和执行任务。
val taskListener = object : TaskScheduler.TaskListener {
override fun beforeExecute(task: TaskScheduler.NamedRunnable) {
println("开始任务:${task.name}")
}
override fun afterExecute(task: TaskScheduler.NamedRunnable, exception: Exception?) {
println("完成任务:${task.name},异常:$exception")
}
}
val scheduler = TaskScheduler(taskListener, 5)
scheduler.submit("加载数据") {
// 加载数据的代码
}
scheduler.submit("处理数据") {
// 处理数据的代码
}
当所有任务完成后,调度器将在指定的超时后自动关闭,确保不浪费资源。
import java.util.concurrent.* import java.util.concurrent.atomic.AtomicBoolean class TaskScheduler(private val listener: TaskListener? = null, private val timeout: Long = 5) { private val taskQueue = ConcurrentLinkedQueue<NamedRunnable>() private val isTaskRunning = AtomicBoolean(false) private var executorService: ScheduledExecutorService? = null @Synchronized fun submit(name: String, task: Runnable) { ensureExecutorService() taskQueue.offer(NamedRunnable(name, task)) if (isTaskRunning.compareAndSet(false, true)) { executorService?.submit { processTasks() } } } private fun processTasks() { try { while (taskQueue.isNotEmpty()) { val nextTask = taskQueue.poll() listener?.beforeExecute(nextTask) var exception: Exception? = null try { nextTask.run() } catch (e: Exception) { exception = e } listener?.afterExecute(nextTask, exception) } } finally { isTaskRunning.set(false) scheduleShutdown() } } private fun ensureExecutorService() { if (executorService == null || executorService!!.isShutdown) { executorService = Executors.newSingleThreadScheduledExecutor() println("ensureExecutorService newSingleThreadScheduledExecutor") } } private fun scheduleShutdown() { executorService?.schedule({ if (taskQueue.isEmpty() && isTaskRunning.compareAndSet(false, true)) { executorService?.shutdown() executorService = null println("scheduleShutdown shutdown") } else { isTaskRunning.set(false) // 确保新任务可以触发执行器重启 } }, timeout, TimeUnit.SECONDS) } interface TaskListener { fun beforeExecute(task: NamedRunnable) fun afterExecute(task: NamedRunnable, exception: Exception?) } class NamedRunnable(val name: String, private val task: Runnable) : Runnable { override fun run() { task.run() } } }
简要概括下优缺点:
优点:
缺点:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。