Android Runnable运行在哪个线程
2016-05-26 15:32
441 查看
Android Runnable运行在哪个线程
Android中的Runnable并不一定是新开的线程,比如下面调用的方法就是运行在UI主线程中
Hanlder handler = new Handler();
handler.post(new Runnable(){
public void run(){
}
});
官方文档对此的解释是:
The runnable will be run on the user interface thread. ”
boolean Android.view.View .post(Runnable action)
Causes the Runnable to be added to the message queue. The runnable will be run on the user interface thread.
Parameters:
action The Runnable that will be executed.
Returns:
Returns true if the Runnable was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting.
我们可以通过handler的对象的post方法,把Runnable对象(一般是Runnable的子类)传过去,handler会在Looper中调用Runnable的run方法执行,Runnable是一个接口,不是一个线程,一般线程会实现Runnable接口
这里我们看代码handler.post(new Runnable(){好像是new了一个interface,其实是new一个实现Runnable的匿名内部类(Inner Anoymous Class)}) 这是一个简练的方法
Runnalbe是一个接口,不是一个线程,一般线程会实现Runnalbe接口,所以如果我们使用匿名内部类是运行在UI主线程的,如果我们使用实现这个Runnable接口的线程类,则是运行在对应的线程的。
具体来说这个函数的工作原理如下:
View.post(Runnalbe)方法,在post(Runanble action)方法中,View获得当前主线程(即UI线程)的handler,然后将action对象post到handler里面去,在Handler里,它将传递过来的action对象封装成一个Message(Message 的callback为action),然后将其投入到UI线程的消息循环中,在handler再次处理该Message时,有一条分支(未解释的那条)就是为它所设,直接调用runnable的run方法,而此时,已经路由到UI线程里,因此我们可以毫无顾虑来更新UI。
如下图,前面看到的代码,我们这里的Message的callback为一个Runnalbe的匿名内部类,这种情况下,由于不是在新的线程中使用,所以千万别做复杂的计算逻辑。
Android中的Runnable并不一定是新开的线程,比如下面调用的方法就是运行在UI主线程中
Hanlder handler = new Handler();
handler.post(new Runnable(){
public void run(){
}
});
官方文档对此的解释是:
The runnable will be run on the user interface thread. ”
boolean Android.view.View .post(Runnable action)
Causes the Runnable to be added to the message queue. The runnable will be run on the user interface thread.
Parameters:
action The Runnable that will be executed.
Returns:
Returns true if the Runnable was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting.
我们可以通过handler的对象的post方法,把Runnable对象(一般是Runnable的子类)传过去,handler会在Looper中调用Runnable的run方法执行,Runnable是一个接口,不是一个线程,一般线程会实现Runnable接口
这里我们看代码handler.post(new Runnable(){好像是new了一个interface,其实是new一个实现Runnable的匿名内部类(Inner Anoymous Class)}) 这是一个简练的方法
Runnalbe是一个接口,不是一个线程,一般线程会实现Runnalbe接口,所以如果我们使用匿名内部类是运行在UI主线程的,如果我们使用实现这个Runnable接口的线程类,则是运行在对应的线程的。
具体来说这个函数的工作原理如下:
View.post(Runnalbe)方法,在post(Runanble action)方法中,View获得当前主线程(即UI线程)的handler,然后将action对象post到handler里面去,在Handler里,它将传递过来的action对象封装成一个Message(Message 的callback为action),然后将其投入到UI线程的消息循环中,在handler再次处理该Message时,有一条分支(未解释的那条)就是为它所设,直接调用runnable的run方法,而此时,已经路由到UI线程里,因此我们可以毫无顾虑来更新UI。
如下图,前面看到的代码,我们这里的Message的callback为一个Runnalbe的匿名内部类,这种情况下,由于不是在新的线程中使用,所以千万别做复杂的计算逻辑。
![](http://img.my.csdn.net/uploads/201212/26/1356512011_9810.png)
相关文章推荐
- 玩转Android之二维码生成与识别
- 玩转Android之二维码生成与识别
- 玩转Android之二维码生成与识别
- 玩转Android之二维码生成与识别
- Android中MVC设计模式的构成
- Android 效果、第三库
- android md5加密
- Android deep links技术实现
- android 4.0以上屏蔽home键,返回键等键所在一栏两种方法。
- android AES加密
- 关于android4.3 Intel X86 Atom System Image的下载
- android ndk开发之代码提示
- android 3des 加密
- android MVP
- android studio发布公共类库到服务器maven仓库
- 关于Activity生命周期的一些笔记
- ViewPager与RadioGroup制作导航栏
- android双缓冲应用
- Android之SurfaceView
- android DES加密