Handler 和 Looper 来满足线程间的通信
来源:互联网 发布:苏州 青少年编程 编辑:程序博客网 时间:2024/05/20 21:48
andriod提供了 Handler 和 Looper 来满足线程间的通信。为了研究其中线程机制的问题,写了2个demo:
Demo1:
package com.mp;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
public class MyThread extends Activity {
private Handler handler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler.post(new MyRunnable());
System.out.println("Oncreate---The Thread id is :"
+ Thread.currentThread().getId());
setContentView(R.layout.main);
}
private class MyRunnable implements Runnable {
public void run() {
System.out.println("Runnable---The Thread is running");
System.out.println("Runnable---The Thread id is :"
+ Thread.currentThread().getId());
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
在这个demo中,整个过程如下:
程序已启动,就把MyRunnable加入到消息队列中,android的handler是异步机制,所以在handler.post(new MyRunnable()); 之后,程序会继续执行,所以以后的语句会继续,这时候我们输出Oncreate中的当前线程ID。同时MyRunnable的run方法也在运行,一样输出run方法中的当前线程ID,然后让线程休眠6秒。
demo的结果分析:
1:控制台的输出: Oncreate---The Thread id is :1
Runnable---The Thread is running
Runnable---The Thread id is :1
2:程序启动后6秒,我们才看到main.xml中的内容(只有一个textview)
这2个结果都表明handler和主线程是同一个线程。如果这时候你做的是一个耗时的操作(比如下载),那么这样是不可行的。
于是,android给我们提供了Looper这样一个类。其实Android中每一个Thread都跟着一个Looper,Looper可以帮助Thread维护一个消息队列.
Demo2:
package com.mp;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
public class MyThread2 extends Activity {
private Handler handler = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
HandlerThread handlerThread = new HandlerThread("myHandlerThread");
handlerThread.start();
handler = new Handler(handlerThread.getLooper());
handler.post(new MyRunnable());
System.out.println("Oncreate---The Thread id is :"
+ Thread.currentThread().getId());
setContentView(R.layout.main);
}
private class MyRunnable implements Runnable {
public void run() {
System.out.println("Runnable---The Thread is running");
System.out.println("Runnable---The Thread id is :"
+ Thread.currentThread().getId());
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
在这个demo中,用到了HandlerThread,在HandlerThread对象中可以通过getLooper方法获取一个Looper对象控制句柄,我们可以将其这个Looper对象映射到一个Handler中去来实现一个线程同步机制。于是就有以下结果;
1:控制台的输出: Oncreate---The Thread id is :1
Runnable---The Thread is running
Runnable---The Thread id is :10
2:程序启动后,我们立刻看到main.xml中的内容。
这样就达到了多线程的结果。
Demo1:
package com.mp;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
public class MyThread extends Activity {
private Handler handler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler.post(new MyRunnable());
System.out.println("Oncreate---The Thread id is :"
+ Thread.currentThread().getId());
setContentView(R.layout.main);
}
private class MyRunnable implements Runnable {
public void run() {
System.out.println("Runnable---The Thread is running");
System.out.println("Runnable---The Thread id is :"
+ Thread.currentThread().getId());
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
在这个demo中,整个过程如下:
程序已启动,就把MyRunnable加入到消息队列中,android的handler是异步机制,所以在handler.post(new MyRunnable()); 之后,程序会继续执行,所以以后的语句会继续,这时候我们输出Oncreate中的当前线程ID。同时MyRunnable的run方法也在运行,一样输出run方法中的当前线程ID,然后让线程休眠6秒。
demo的结果分析:
1:控制台的输出: Oncreate---The Thread id is :1
Runnable---The Thread is running
Runnable---The Thread id is :1
2:程序启动后6秒,我们才看到main.xml中的内容(只有一个textview)
这2个结果都表明handler和主线程是同一个线程。如果这时候你做的是一个耗时的操作(比如下载),那么这样是不可行的。
于是,android给我们提供了Looper这样一个类。其实Android中每一个Thread都跟着一个Looper,Looper可以帮助Thread维护一个消息队列.
Demo2:
package com.mp;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
public class MyThread2 extends Activity {
private Handler handler = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
HandlerThread handlerThread = new HandlerThread("myHandlerThread");
handlerThread.start();
handler = new Handler(handlerThread.getLooper());
handler.post(new MyRunnable());
System.out.println("Oncreate---The Thread id is :"
+ Thread.currentThread().getId());
setContentView(R.layout.main);
}
private class MyRunnable implements Runnable {
public void run() {
System.out.println("Runnable---The Thread is running");
System.out.println("Runnable---The Thread id is :"
+ Thread.currentThread().getId());
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
在这个demo中,用到了HandlerThread,在HandlerThread对象中可以通过getLooper方法获取一个Looper对象控制句柄,我们可以将其这个Looper对象映射到一个Handler中去来实现一个线程同步机制。于是就有以下结果;
1:控制台的输出: Oncreate---The Thread id is :1
Runnable---The Thread is running
Runnable---The Thread id is :10
2:程序启动后,我们立刻看到main.xml中的内容。
这样就达到了多线程的结果。
0 0
- Handler 和 Looper 来满足线程间的通信
- 线程间通信Handler,Looper,MessageQueue和Queue
- Handler,Looper用法和主线程子线程间通信
- android开发 handler和looper 线程通信
- Android的Looper,Handler以及线程间的通信
- Android的Looper,Handler以及线程间的通信
- Android的Looper,Handler以及线程间的通信
- handler实现线程间通信的原理-looper
- Android:Handler+Looper+MessageQueue+Thread(线程间的通信)随记
- Android线程间通信机制(Handler Looper )
- Android线程间通信机制(Handler Looper )
- Android线程间通信方式:Handler Looper
- android handler,looper子线程间通信
- Android线程间通信 Handler、Looper、MessageQueue
- handler looper 和 线程
- 线程中Handler和Looper的理解
- Android线程通信:Handler,MessageQueue和Looper原理分析
- Android--Handler+Looper+MessageQueue+Thread线程之间的通信
- 纯基于PHP的单点登陆
- vs 生成时提示obj的exe被占用
- 提高SQL查询效率 的10大方法
- Android客户端代码保护技术-完整性校验
- Android入门教程 Service的用法
- Handler 和 Looper 来满足线程间的通信
- 读深入理解Linux内核 (前言和第一章)
- Inflate()---Android之Inflate()方法用途
- LeetCode—400. Nth Digit
- @RequestMapping注解方法返回值意义
- Centos7 Nexus Maven 开机启动
- Cocos2d-x下Lua调用自定义C++类和函数的最佳实践
- nodejs学习-post请求处理
- CSS:CSS概述与引用