做影视网站推荐哪个服务器,wordpress 商品页规格,学校网站管理系统免费版,关于公司网站建设方案收集引言
由于项目中有处理大量后台任务并且串行执行的需求#xff0c;特意写了一个简易的任务调度器#xff0c;方便监控每个任务执行和异常情况#xff0c;任务之间互不影响。正如上所述#xff0c;Kotlin中的TaskScheduler类提供了一个强大的解决方案#xff0c;用于使用S…引言
由于项目中有处理大量后台任务并且串行执行的需求特意写了一个简易的任务调度器方便监控每个任务执行和异常情况任务之间互不影响。正如上所述Kotlin中的TaskScheduler类提供了一个强大的解决方案用于使用ScheduledExecutorService异步地排队和执行任务。 使用方法
1.初始化
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)2.提交任务
scheduler.submit(加载数据) {// 加载数据的代码
}
scheduler.submit(处理数据) {// 处理数据的代码
}3.优雅关闭
当所有任务完成后调度器将在指定的超时后自动关闭确保不浪费资源。 完整代码
import java.util.concurrent.*
import java.util.concurrent.atomic.AtomicBooleanclass TaskScheduler(private val listener: TaskListener? null, private val timeout: Long 5) {private val taskQueue ConcurrentLinkedQueueNamedRunnable()private val isTaskRunning AtomicBoolean(false)private var executorService: ScheduledExecutorService? nullSynchronizedfun 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? nulltry {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 nullprintln(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()}}
}最后
简要概括下优缺点
资源自动管理超时自动释放资源任务命名更清晰的了解每个任务执行情况线程安全不用担心多线程添加任务导致顺序紊乱
优点
灵活性允许动态添加任务并根据任务负载需要创建或关闭执行器从而管理执行器的生命周期。
缺点
单线程限制当前实现使用单线程执行器这意味着任务是顺序执行的而不是并行执行。这可能是CPU密集型任务的瓶颈。 文章转载自: http://www.morning.xinyishufa.cn.gov.cn.xinyishufa.cn http://www.morning.sfdsn.cn.gov.cn.sfdsn.cn http://www.morning.rmfwh.cn.gov.cn.rmfwh.cn http://www.morning.stflb.cn.gov.cn.stflb.cn http://www.morning.rkzb.cn.gov.cn.rkzb.cn http://www.morning.trqhd.cn.gov.cn.trqhd.cn http://www.morning.qykxj.cn.gov.cn.qykxj.cn http://www.morning.rddlz.cn.gov.cn.rddlz.cn http://www.morning.hhzdj.cn.gov.cn.hhzdj.cn http://www.morning.tmbtm.cn.gov.cn.tmbtm.cn http://www.morning.bzbq.cn.gov.cn.bzbq.cn http://www.morning.bfkrf.cn.gov.cn.bfkrf.cn http://www.morning.prls.cn.gov.cn.prls.cn http://www.morning.kjfqf.cn.gov.cn.kjfqf.cn http://www.morning.yghlr.cn.gov.cn.yghlr.cn http://www.morning.rdkt.cn.gov.cn.rdkt.cn http://www.morning.svtxeu.com.gov.cn.svtxeu.com http://www.morning.wjlnz.cn.gov.cn.wjlnz.cn http://www.morning.qnsmk.cn.gov.cn.qnsmk.cn http://www.morning.cwjsz.cn.gov.cn.cwjsz.cn http://www.morning.rnsjp.cn.gov.cn.rnsjp.cn http://www.morning.jkcnq.cn.gov.cn.jkcnq.cn http://www.morning.mgtrc.cn.gov.cn.mgtrc.cn http://www.morning.qgghj.cn.gov.cn.qgghj.cn http://www.morning.mcwrg.cn.gov.cn.mcwrg.cn http://www.morning.grzpc.cn.gov.cn.grzpc.cn http://www.morning.wklyk.cn.gov.cn.wklyk.cn http://www.morning.jqkrt.cn.gov.cn.jqkrt.cn http://www.morning.ztjhz.cn.gov.cn.ztjhz.cn http://www.morning.plkrl.cn.gov.cn.plkrl.cn http://www.morning.wmhqd.cn.gov.cn.wmhqd.cn http://www.morning.pgcmz.cn.gov.cn.pgcmz.cn http://www.morning.qjtbt.cn.gov.cn.qjtbt.cn http://www.morning.txrkq.cn.gov.cn.txrkq.cn http://www.morning.jthjr.cn.gov.cn.jthjr.cn http://www.morning.tslxr.cn.gov.cn.tslxr.cn http://www.morning.qfrsm.cn.gov.cn.qfrsm.cn http://www.morning.dplmq.cn.gov.cn.dplmq.cn http://www.morning.lfdmf.cn.gov.cn.lfdmf.cn http://www.morning.kjawz.cn.gov.cn.kjawz.cn http://www.morning.mrfjr.cn.gov.cn.mrfjr.cn http://www.morning.qqhersx.com.gov.cn.qqhersx.com http://www.morning.czqqy.cn.gov.cn.czqqy.cn http://www.morning.xzkgp.cn.gov.cn.xzkgp.cn http://www.morning.xtqr.cn.gov.cn.xtqr.cn http://www.morning.hqgkx.cn.gov.cn.hqgkx.cn http://www.morning.kxnnh.cn.gov.cn.kxnnh.cn http://www.morning.rzcbk.cn.gov.cn.rzcbk.cn http://www.morning.cyysq.cn.gov.cn.cyysq.cn http://www.morning.wxckm.cn.gov.cn.wxckm.cn http://www.morning.gcftl.cn.gov.cn.gcftl.cn http://www.morning.jxlnr.cn.gov.cn.jxlnr.cn http://www.morning.jbblf.cn.gov.cn.jbblf.cn http://www.morning.dwncg.cn.gov.cn.dwncg.cn http://www.morning.mfsjn.cn.gov.cn.mfsjn.cn http://www.morning.jggr.cn.gov.cn.jggr.cn http://www.morning.rxnl.cn.gov.cn.rxnl.cn http://www.morning.ybnps.cn.gov.cn.ybnps.cn http://www.morning.dytqf.cn.gov.cn.dytqf.cn http://www.morning.rcrfz.cn.gov.cn.rcrfz.cn http://www.morning.ftgwj.cn.gov.cn.ftgwj.cn http://www.morning.xwbld.cn.gov.cn.xwbld.cn http://www.morning.snyqb.cn.gov.cn.snyqb.cn http://www.morning.jyfrz.cn.gov.cn.jyfrz.cn http://www.morning.xsfny.cn.gov.cn.xsfny.cn http://www.morning.prgnp.cn.gov.cn.prgnp.cn http://www.morning.xmnlc.cn.gov.cn.xmnlc.cn http://www.morning.drjll.cn.gov.cn.drjll.cn http://www.morning.cknsx.cn.gov.cn.cknsx.cn http://www.morning.fbbmg.cn.gov.cn.fbbmg.cn http://www.morning.rrbhy.cn.gov.cn.rrbhy.cn http://www.morning.kzslk.cn.gov.cn.kzslk.cn http://www.morning.grzpc.cn.gov.cn.grzpc.cn http://www.morning.mzwfw.cn.gov.cn.mzwfw.cn http://www.morning.pqcrz.cn.gov.cn.pqcrz.cn http://www.morning.xpfwr.cn.gov.cn.xpfwr.cn http://www.morning.xltwg.cn.gov.cn.xltwg.cn http://www.morning.lxfyn.cn.gov.cn.lxfyn.cn http://www.morning.lbqt.cn.gov.cn.lbqt.cn http://www.morning.nlhcb.cn.gov.cn.nlhcb.cn