Android 用线程池实现一个简单的任务队列(Kotlin)

关于线程池,Kotlin和java的使用方式一样

在Android中,很多人喜欢用Handler的postDelayed() 去实现延时任务.

要使用postDelayed(),去实现延时任务队列,就不可避免要使用递归.

但是这样做,代码的简洁性,和书写的简易,就远不如使用线程池.

使用线程池的简单程度:

Kotlin 复制代码
    private val mThreadPool = Executors.newSingleThreadScheduledExecutor()
    private val mThreadTime = 150  //任务间隔! 毫秒
    fun writeData() {
        mThreadPool.schedule({ //添加任务
           
        }, mThreadTime.toLong(), TimeUnit.MILLISECONDS) //延时时长,时间单位
    }
    
    //使用完线程池别忘记关闭
    mThreadPool.shutdown()
相关推荐
Kapaseker7 小时前
Kotlin 相等的奥义
android·kotlin
Lyyaoo.7 小时前
【MySQL】锁机制
android·数据库·mysql
DS随心转插件7 小时前
DeepSeek 代码手机端导出与 AI 辅助方案实测
android·人工智能·chatgpt·智能手机·deepseek·ai导出鸭
JohnnyDeng947 小时前
【Android】Flow vs LiveData:选型指南与迁移实践
android·kotlin·livedata·flow
plainGeekDev7 小时前
线程安全集合 → 协程安全替代
android·java·kotlin
zhangphil7 小时前
Kotlin管道Channel构造函数参数capacity值RENDEZVOUS与UNLIMITED
android·kotlin
plainGeekDev8 小时前
Timer → Coroutines
android·java·kotlin
Coffeeee8 小时前
Android17应用内存限制--App:我人不舒服,系统:那你走吧
android·google·kotlin
问心无愧05138 小时前
ctf show web入门101
android·前端·笔记
一池秋_8 小时前
chroot-debian一键部署
android·容器·debian