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()
相关推荐
冉冉同学4 小时前
AI Agent 开始操作真实手机:移动端自动化的 3 个新考点
android·ai编程
帅得不敢出门5 小时前
Android Framework关闭软件低电关机功能测试PMIC低电保护
android
熊猫钓鱼>_>5 小时前
Kotlin Multiplatform for OpenHarmony 实战:为 Landscapist 实现图片加载适配
开发语言·kotlin·华为云·ai编程·harmonyos·鸿蒙·openharmony
恋猫de小郭5 小时前
Flutter + EmbeddingGemma 2,谷歌发布完全端侧的 AI Edge Foresight
android·前端·flutter
ZealSinger6 小时前
Kotlin2.4迁移context parameters
kotlin·协程·迁移·kotlin2.4
墨天梦7 小时前
B16_Material主题与可访问交互
android·kotlin·交互
弯_弯7 小时前
企业知识库 RAG 落地:从 Demo 到生产,四条工程链路与参数级自查
android·java·javascript
晚风叙码9 小时前
MySQL 表的约束详解:从空属性到外键
android·数据库·mysql·adb
alexhilton19 小时前
模块化Jetpack Compose架构
android·kotlin·android jetpack
厮年断弦21 小时前
AI 辅助开发实录:一周做完一个 Android 本地音乐播放器,这些坑替你踩了
android