handler.post()方法的执行时机

本文通过一个具体的示例,详细解析了Android中Handler的工作机制。解释了为何在Activity的onCreate方法中通过Handler.post提交的任务会在所有当前任务完成后执行,揭示了Android主线程的消息队列(MessageQueue)如何工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有如下场景,在onCreate()方法里执行handler.post(new Runnable())

public class ProgressBarActivity extends Activity {
private final static String TAG = "ProgressBarActivity";

private Runnable test = new Runnable(){
    @Override
    public void run() {
        try {
            Thread.sleep(10000);
            Log.i(TAG,"Thread---->"+Thread.currentThread().getId());
            Log.i(TAG,"Thread---->"+Thread.currentThread().getName());
        } catch (InterruptedException e) {}
    }
};
private Handler handler = new Handler(){
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    handler.post(test);
    this.setContentView(R.layout.progress_bar_layout);
    Log.i(TAG,"Activity--->"+Thread.currentThread().getId());
    Log.i(TAG,"Activity--->"+Thread.currentThread().getName());
} 

以上代码里的test会立马执行或者说在activity的log日志前执行吗?
看下打印结果:

06-04 14:13:09.964: INFO/ProgressBarActivity(366): Activity--->1

06-04 14:13:09.964: INFO/ProgressBarActivity(366): Activity--->main

06-04 14:13:20.070: INFO/ProgressBarActivity(366): Thread---->1

06-04 14:13:20.070: INFO/ProgressBarActivity(366): Thread---->main

都知道post(new Runnable())也是在主线程里执行的,那为什么从打印结果上看是先走完oncreate方法再执行runnable里的方法呢?这个要从handler原理说起:

  • 当Android应用程序启动时,framework会为该应用程序的主线程创建一个Looper对象。这个Looper对象包含一个简单的消息队列Message Queue,并且能够循环的处理队列中的消息。这些消息包括大多数应用程序framework事件,例如Activity生命周期方法调用、button点击等,这些消息都会被添加到消息队列中并被逐个处理。

所以Activity生命周期方法调用其实也是通过handler来处理的,oncreate调用和post(new Runnable())对于handler来说都是一个message的处理,都存在主线程的message queue里,但队列是FIFO原则,所以Runnable()必须等oncreate这条message处理完才能处理自己的这条message。

You have it wrong: the docs here say this:

Causes the Runnable r to be added to the message queue. The runnable will be run on the thread to which this handler is attached. 
That means the Runnable will be run on the Activity's main thread, but will be queued up and run AFTER the thread is done with its current work. The current work is completing onCreate, so after onCreate finishes the thread is now free and will process the Runnable. This is why you see the Activity text before the Runnable text: the current thread can not just stop what its doing and pick up the Runnable.

关于以上demo相关的stackoverflow地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值