参考文章:http://www.cnblogs.com/hnrainll/p/3597246.html
一、HandlerThread简介
首先我们来看看为什么我们要使用HandlerThread?
在我们的应用程序当中为了实现同时完成多个任务,所以我们会在应用程序当中创建多个线程。为了让多个线程之间能够方便的通信,我们会使用Handler实现线程间的通信。
下面我们看看如何在线程当中实例化Handler。在线程中实例化Handler我们需要保证线程当中包含Looper(注意:UI线程默认包含Looper)。
为线程创建Looper的方法如下:在线程run()方法当中先调用Looper.prepare()初始化Looper,然后再run()方法最后调用Looper.loop(),这样我们就在该线程当中创建好Looper。(注意:Looper.loop()方法默认是死循环)
我们实现Looper有没有更加简单的方法呢?当然有,这就是我们的HandlerThread。我们来看下Android对HandlerThread的描述。
Handy class for starting a new thread that has a looper. The looper can then be used to create handler classes. Note that start() must still be called.
HandlerThread其本质就是一个线程,只是在其run函数内部已经帮我们实现了Looper.prepare以及Looper.loop的操作,其run函数源码:
public void run() {
mTid = Process.myTid();
Looper.prepare();
synchronized (this) {
mLooper = Looper.myLooper();
notifyAll();
}
Process.setThreadPriority(mPriority);
onLooperPrepared();
Looper.loop();
mTid = -1;
}
2.1 创建一个HandlerThread,即创建了一个包含Looper的线程。
HandlerThread handlerThread = new HandlerThread("leochin.com");
handlerThread.start(); //创建HandlerThread后一定要记得start()
2.2 获取HandlerThread的Looper
Looper looper = handlerThread.getLooper();
2.3创建Handler,通过Looper初始化
Handler handler = new Handler(looper);
通过以上三步我们就成功创建HandlerThread。通过handler发送消息,就会在子线程中执行。
三、应用举例
3.1 使用Handler处理事件
package com.debby.threaddemo;
import android.app.Activity;
import android.content.AsyncQueryHandler;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
public class ThreadDemo extends Activity {
private static final String TAG = "bb";
private int count = 0;
private Handler mHandler ;
private Runnable mRunnable = new Runnable() {
public void run() {
//为了方便 查看,我们用Log打印出来
Log.e(TAG, Thread.currentThread().getId() + " " +count);
count++;
setTitle("" +count);
//每2秒执行一次
mHandler.postDelayed(mRunnable, 2000);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
Log.e(TAG, "Main id "+Thread.currentThread().getId() + " " +count);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//通过Handler启动线程
mHandler = new Handler();
mHandler.post(mRunnable);
}
@Override
protected void onDestroy() {
//将线程与当前handler解除绑定
//mHandler.removeCallbacks(mRunnable);
super.onDestroy();
}
}
最后得到mHandler.post(mRunnable),将执行该Runnable线程体,通过打印得出它其实和UI线程属于同一线程,如果该线程体中执行的是耗时的操作,将严重影响用户和UI线程的交互。
3.2 使用HandlerThread
public class ThreadDemo extends Activity {
private static final String TAG = "bb";
private int count = 0;
private Handler mHandler ;
private Runnable mRunnable = new Runnable() {
public void run() {
//为了方便 查看,我们用Log打印出来
Log.e(TAG, Thread.currentThread().getId() + " " +count);
count++;
// setTitle("" +count);
//每2秒执行一次
mHandler.postDelayed(mRunnable, 2000);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
Log.e(TAG, "Main id "+Thread.currentThread().getId() + " " +count);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//通过Handler启动线程
HandlerThread handlerThread = new HandlerThread("threadone");//创建一个handlerThread线程
handlerThread.start();//启动该线程
mHandler = new Handler(handlerThread.getLooper());//将子线程的消息循环,赋值给主线程的handler
mHandler.post(mRunnable); //加入mRunnable线程体到子线程的消息队列
}
@Override
protected void onDestroy() {
//将线程与当前handler解除
mHandler.removeCallbacks(mRunnable);
super.onDestroy();
}
}
其实现在的线程体和UI主线程不是同一个线程了,这样当执行复杂的操作时就不会影响到UI主线程的交互。
相关文章:
http://blog.csdn.net/shaozg168/article/details/16940687