Android 线程简单分析(一) Android 并发之synchronized锁住的是代码仍是对象(二) Android 并发之CountDownLatch、CyclicBarrier的简单应用(三) Android 并发HashMap和ConcurrentHashMap的简单应用(四)(待发布) Android 并发之Lock、ReadWriteLock和Condition的简单应用(五) Android 并发之CAS(原子操做)简单介绍(六) Android 并发Kotlin协程的重要性(七)(待发布) Android 并发之AsyncTask原理分析(八) Android 并发之Handler、Looper、MessageQueue和ThreadLocal消息机制原理分析(九) Android 并发之HandlerThread和IntentService原理分析(十)并发
首先HandlerThread是一个线程而不是Handler,为了讲解HandlerThread,因此不得不提Looper轮训器,若是看过Handler源码的同窗应该很清楚,Handler 发送消息到Looper的MessageQueue中,而后Looper的loop方中不断轮训的从MessageQueue中取出消息,并分发给Handler处理,即loop方法在那个线程调用,那么消息的处理就在那个险种中:ide
for (;;) {
Message msg = queue.next(); // might block
if (msg == null) {
//没有消息代表消息队列正在退出。
return;
}
//消息分发,Message的target持有指定Handler的的引用,分发给指定的Handler处理消息
msg.target.dispatchMessage(msg);
//回收message并放入消息池中
msg.recycleUnchecked();
}
复制代码
明白的Looper的做用以后,接下看看HandlerThread,应该很好理解,HandlerThread继承Thread,不是一个Handler,其封装了Handler、Thread,官方是这样解析这个类,一个很方便的用于启动一个具备Looper的新线程。 而后能够使用这个Looper建立Handler类。必需要先调用start方法:oop
@Override
public void run() {
mTid = Process.myTid();
Looper.prepare();
synchronized (this) {
mLooper = Looper.myLooper();
notifyAll();//唤醒等待的线程
}
Process.setThreadPriority(mPriority);
onLooperPrepared();
Looper.loop();
mTid = -1;
}
public Looper getLooper() {
if (!isAlive()) {
return null;
}
// 若是线程已经启动,一直等待知道Looper被建立。
synchronized (this) {
while (isAlive() && mLooper == null) {
try {
wait();
} catch (InterruptedException e) {
}
}
}
return mLooper;
}
复制代码
这两个就是主要的代码,因此我前面说了,必需要调用start方法启动线程,在run方法中建立Looper,这样咱们若是须要在子线程中处理消息,能够使用这个Looper来建立Handler, getLooper()方法就是拿到HandlerThread的建立Looper;ui
###IntentService:this
讲完HandlerThread,必然就是IntentService了,其也是一个Service,只是其在工做线程中运行,可是他是每次提交任务,也是在同一个线程中串行执行,IntentService其分装了HandlerThread,并使用HandlerThread的Looper做为处理消息的线程:spa
@Override
public void onCreate() {
super.onCreate();
HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
复制代码
在Oncreate生命周期方法中建立了HandlerThread ,并使用期Looper建立ServiceHandler,service的声明周期是:首次建立会调用oncreate方法,以后都会调用onStartCommand->onStart因此:线程
@Override
public void onStart(@Nullable Intent intent, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
mServiceHandler.sendMessage(msg);
}
复制代码
每次启动IntentService都会调用mServiceHandler.sendMessage(msg);发消息,并在handler处理消息,因此这些消息都是串行执行的:code
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
onHandleIntent((Intent)msg.obj);
stopSelf(msg.arg1);
}
}
复制代码
当onHandleIntent方法执行结束以后,IntentService会尝试经过stopSelf(int startId)来尝试中止服务。之因此不用stopSelf()来中止服务,是由于stopSelf()会马上中止服务,而stopSelf(int startId)则会等待全部的消息都处理完毕才回终止服务。通常来讲,stopSelf(int startId)在尝试中止服务以前会判断最近启动服务的次数是否和startId相等,若是相等则马上中止服务。协程
若是服务中止,会把全部的消息清除对象
@Override
public void onDestroy() {
mServiceLooper.quit();
}
复制代码
为何不建议经过 bindService() 启动 IntentService?
固然若是你要是同时使用startService()启动服务、BindService()绑定服务的生命周期,那我就无话可说了,我是以为可行,具体需求我没用过,若是哪位同窗有这种需求,能够讨论哦。