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()
相关推荐
阿pin8 小时前
Android随笔-kotlin withContext
android·kotlin·withcontext
树码小子9 小时前
开启 IDEA 中的断言功能
android·java·intellij-idea
Kslient9 小时前
HeaderBehavior
android
Android-Flutter10 小时前
android 动画详解
android·kotlin
小孔龙15 小时前
GraphicBuffer 跨进程共享
android
行业研究员16 小时前
Android内置GPS与腾讯地图定位技术对比分析
android·android定位·腾讯地图定位
Android-Flutter16 小时前
android WMS 详解(二)
android·kotlin
游戏开发爱好者817 小时前
App Store 上传 IPA 自动化,.p8 密钥认证与 CI/CD 接入实战
android·运维·ci/cd·小程序·uni-app·自动化·iphone
游戏开发爱好者818 小时前
iOS 推送怎么配置,APNs 推送证书、设备库与群发
android·ios·小程序·https·uni-app·iphone·webview
阿pin19 小时前
Android随笔-kotlin 高阶函数
android·kotlin·高阶函数