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()
相关推荐
私人珍藏库1 小时前
[Android] 一木百宝箱-万能百宝箱+抖音去水印等几百种功能
android·人工智能·app·软件·多功能
wupa2 小时前
在 enableEdgeToEdge 模式下处理软键盘与自定义面板协同
android
plainGeekDev3 小时前
ProGuard → R8
android·java·kotlin
雨白4 小时前
C 语言字符串:从字符数组、指针到核心操作实战
android
Java小白笔记5 小时前
MySQL中存储过程大表分批删除历史数据
android·mysql·adb
aidou13145 小时前
Kotlin中沉浸式状态栏显示布局
android·kotlin·windowmanager·沉浸式状态栏·insetslistener·insetscompat·systembars
我命由我123456 小时前
Android 构建项目问题:XML 片段出错,com.ctc.wstx.exc.WstxUnexpectedCharException
android·xml·android studio·android jetpack·android-studio·android runtime
阿pin6 小时前
Android随笔-pipe是什么?
android·linux·pipe
帅次6 小时前
Android 高级工程师面试:Flow 与状态流 近1年高频追问 22 题
android·面试·职场和发展
雨季余静6 小时前
RustDesk Android APK 从零编译全步骤
android