• CountDownTimer的用法及原理


    1.主线程中使用

    值得注意的是,CountDownTimer可以在主线程中直接使用。验证一下回调的执行线程。在主线程中执行如下代码

    CountDownTimer countDownTimer = new CountDownTimer(1000L * 30, 1000L) {
                @Override
                public void onTick(long millisUntilFinished) {
                    // your logic for tick
                    Log.i("TEST", "onTick - " + Thread.currentThread() + ", id - " + Thread.currentThread().getId());
                }
     
                @Override
                public void onFinish() {
                    // your logic for finish
     
                    Log.i("TEST", "onFinish" + Thread.currentThread() + ", id - " + Thread.currentThread().getId());
                }
    };
    countDownTimer.start();

    Log:

    02-24 14:05:13.545 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:14.546 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:15.548 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:16.550 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:17.551 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:18.553 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:19.555 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:20.557 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:21.560 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:22.563 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:23.566 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:24.569 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:25.571 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:26.573 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:27.575 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:28.578 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:29.580 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:30.581 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:31.583 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:32.586 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:33.588 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:34.589 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:35.592 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:36.595 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:37.598 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:38.599 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:39.601 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:40.602 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:41.604 30358 30358 I TEST    : onTick - Thread[main,5,main], id - 1
    02-24 14:05:43.546 30358 30358 I TEST    : onFinishThread[main,5,main], id - 1

    可见,回调执行在主线程,这也就意味着可以在回调中直接处理UI,还是很方便的。

    2.在工作线程中使用

    不能在工作线程中像主线程一样简单使用的原因,是CountDownTimer是基于Handler的,细节将在原理部分说明。主线程已经由安卓框架层启动了消息循环,所以可以直接使用。那么在工作线程中怎样使用呢?可以借助于HanderThread。在onLooperPrepared()中初始化并开启CountDownTimer:

    new HandlerThread("TestThread") {
        protected void onLooperPrepared() {
            CountDownTimer countDownTimer = new CountDownTimer(1000L * 30, 1000L) {
                @Override
                public void onTick(long millisUntilFinished) {
                    // your logic for tick
                    Log.i("TEST", "onTick - " + Thread.currentThread() + ", id - " + Thread.currentThread().getId());
                }
    
                @Override
                public void onFinish() {
                    // your logic for finish
                    Log.i("TEST", "onFinish" + Thread.currentThread() + ", id - " + Thread.currentThread().getId());
                }
            };
            countDownTimer.start();
        }
    }.start();

    执行log如下:

    02-24 14:03:35.789 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:36.790 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:37.793 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:38.795 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:39.797 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:40.800 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:41.801 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:42.803 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:43.804 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:44.806 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:45.809 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:46.810 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:47.813 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:48.814 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:49.816 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:50.818 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:51.820 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:52.821 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:53.823 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:54.825 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:55.827 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:56.828 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:57.829 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:58.831 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:03:59.832 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:04:00.835 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:04:01.836 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:04:02.838 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:04:03.840 30099 30222 I TEST    : onTick - Thread[TestThread,5,main], id - 473
    02-24 14:04:05.791 30099 30222 I TEST    : onFinishThread[TestThread,5,main], id - 473
  • 相关阅读:
    docker容器跨服务器的迁移的方法
    Docker 更改镜像存储位置
    将Docker容器转移至另一服务器
    docker容器存放目录磁盘空间满了,转移数据修改Docker默认存储位置
    在线|二轮辅导[06][三角函数+解三角形02]
    在线|二轮辅导[05][三角函数+解三角形01]
    推荐|网络画板2D学习笔记
    推荐|网络画板3D学习笔记
    导数法求函数最值
    在线|二轮辅导[04][函数与导数02]
  • 原文地址:https://www.cnblogs.com/ganchuanpu/p/7583838.html
Copyright © 2020-2023  润新知