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()
相关推荐
summerkissyou19874 小时前
Android - 摄像头 - hal - 开发教程,例子,常见问题,分析方法,解决方案
android
summerkissyou19874 小时前
Android 16 架构图
android
神龙天舞20015 小时前
MySQL 备库为什么会延迟好几个小时
android·数据库·mysql
码农coding8 小时前
android12 systemUI 之锁屏
android
圆山猫8 小时前
[Virtualization](三):RISC-V H-extension 与 Guest 执行模式
android·java·risc-v
爱笑鱼9 小时前
Binder(七):getCallingUid() 读到的是谁?clearCallingIdentity() 又清掉了什么?
android
2501_9159090614 小时前
iOS 应用反调试技详解术 检测调试器的原理与防护实践
android·ios·小程序·https·uni-app·iphone·webview
Lvan的前端笔记15 小时前
AndroidX 完全入门指南
android·androidx
帅次15 小时前
Android 应用高级面试:Window 近1年高频追问 18 题
android·面试·职场和发展
总捣什么乱116 小时前
MySQL MySQL是怎么保证主备一致的?
android·mysql·adb