应该了解的知识:
1.什么是进程、什么是线程
2.为什么要使用多线程
3.Handler机制
4.Handler原理
什么是进程、什么是线程
进程是一个程序的完全执行,包括了程序和程序需要的资源,是分配资源的基本单位;线程是独立运行和独立调度的基本单位,一边听歌一边打字一边玩游戏看似是同时进行实际上是CPU运行时候,快速切换资源,让用户感觉是同时进行的,我们可以将线程理解为程序中的一段可执行的代码块。
为什么要使用多线程--》可以参考别人的专门理解的理解:http://blog.csdn.net/itjavawfc/article/details/50360776
1)耗时的操作使用线程,提高应用程序响应
2)并行操作时使用线程,如C/S架构的服务器端并发线程响应用户的请求。
3)多CPU系统中,使用线程提高CPU利用率
4)改善程序结构。一个既长又复杂的进程可以考虑分为多个线程,成为几个独立或半独立的运行部分,这样的程序会利于理解和修改。
2)并行操作时使用线程,如C/S架构的服务器端并发线程响应用户的请求。
3)多CPU系统中,使用线程提高CPU利用率
4)改善程序结构。一个既长又复杂的进程可以考虑分为多个线程,成为几个独立或半独立的运行部分,这样的程序会利于理解和修改。
Handler 讲解
先看handler源码的部分解释:
/**
* A Handler allows you to send and process {@link Message} and Runnable
* objects associated with a thread's {@link MessageQueue}. Each Handler
* instance is associated with a single thread and that thread's message
* queue. When you create a new Handler, it is bound to the thread /
* message queue of the thread that is creating it -- from that point on,
* it will deliver messages and runnables to that message queue and execute
* them as they come out of the message queue.
*
* <p>There are two main uses for a Handler: (1) to schedule messages and
* runnables to be executed as some point in the future; and (2) to enqueue
* an action to be performed on a different thread than your own.
*
* <p>Scheduling messages is accomplished with the
* {@link #post}, {@link #postAtTime(Runnable, long)},
* {@link #postDelayed}, {@link #sendEmptyMessage},
* {@link #sendMessage}, {@link #sendMessageAtTime}, and
* {@link #sendMessageDelayed} methods. The <em>post</em> versions allow
* you to enqueue Runnable objects to be called by the message queue when
* they are received; the <em>sendMessage</em> versions allow you to enqueue
* a {@link Message} object containing a bundle of data that will be
* processed by the Handler's {@link #handleMessage} method (requiring that
* you implement a subclass of Handler).
*
* <p>When posting or sending to a Handler, you can either
* allow the item to be processed as soon as the message queue is ready
* to do so, or specify a delay before it gets processed or absolute time for
* it to be processed. The latter two allow you to implement timeouts,
* ticks, and other timing-based behavior.
*
* <p>When a
* process is created for your application, its main thread is dedicated to
* running a message queue that takes care of managing the top-level
* application objects (activities, broadcast receivers, etc) and any windows
* they create. You can create your own threads, and communicate back with
* the main application thread through a Handler. This is done by calling
* the same <em>post</em> or <em>sendMessage</em> methods as before, but from
* your new thread. The given Runnable or Message will then be scheduled
* in the Handler's message queue and processed when appropriate.
*/
提取信息时:Handler是用来发送并处理消息和runnable线程对象的,同时2)排列一个在不同的线程上执行的操作,而不是自己的。
Handler的原理实际上就是说Android中异步线程的实现原理:
在线程内部有一个或多个Handler对象,外部程序通过改Handler对象向线程发送异步消息,消息经由Handler传递道MessageQueue对象中。线程内部智能包含一个MessageQueue对象,Looper对象像一个抽水机一样,不断的读取线程MessageQueue中的消息,并回掉给Handler对象中的回掉函数handlerMessage().
分析线程内部的Handler、MessageQueue/Looper类的调用过程
1).线程局部存储Thread Local Storage
程序员通过调用Looper类的静态方法prepare()为线程创建MessageQueue对象:看源码
/**Initialize the current thread as a looper.
* This gives you a chance to create handlers that then reference
* this looper, before actually starting the loop. Be sure to call
* {@link #loop()} after calling this method, and end it by calling
* {@link #quit()}.
*/
public static void prepare() {
prepare(true);
}
private static void prepare(boolean quitAllowed) {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper(quitAllowed))
}
我们在看看Looper(quitAllowed)源码:
/**
*Return the {@link MessageQueue} object associated with the current
* thread. This must be called from a thread running a Looper, or a
* NullPointerException will be thrown.
*/
public static @NonNull MessageQueue myQueue() {
return myLooper().mQueue;
}
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
上面的sThreadLocal的类型是ThreadLocal,该类的作用是提供“线程局部存储”也叫线程本地变量.我们希望,当同时从一个线程中引用该变量时,其值总是相同的,不同线程引用该变量的时候,其值是不同的,即我们需要一个作用域为线程的变量的定义,这就是线程变量局部存储。
ThreadLocal就是提供了这种功能的类,Looper内部的sThreadLocal变量是当该进程第一次调用Looper.prepare()时候被复制的,之后该进程中的其它线程调用prepare()函数的时候,sThreadLocak变量就已经被赋值了。sThreadLocak对象内部会根据调用prepare()线程的id保存一个数据对象,这个数据对象就是所谓的“线程局部存储”对象,该对象是通过sThreadLocal的set()方法设置进去的,Looper类保存的这个对象是一个Looper对象。
prepare()函数中首先调用sThreadLocal.get()函数获取该县城对应的Looper对象,如果线程已经存在Looper对象,肯定出错。否则为该线程创建一个新的Looper对象。
为什么一个线程中只能有一个Looper对象呢?因为异步线程需要的,每个Looper对象都会定义一个MessageQueue对象,一个异步线程只能有一个消息队列,所有只有一个Looper对象。
讲那么多理论的东西可能都绕地球好多圈了,不太理解:其实在一个线程中的Looper对象创建MessageQueue中的过程中即执行Looper.praper()方法的时候,使用sThreadLocal来找该设置Looper对象到线程中去,通过线程的id设置道sThreadLoad对象中去。一个线程中就只有一个Looper对象了。prepare()首先调用sThreadLocal().get()获得该线程的Looperd对象.若果存在直接报异常,不存在直接创建,这样保证每个线程只有一个Looper对象和MessageQueue对象.
Looper
Looper作用:第一、为该类的静态函数prepare()线程创建一个消息队列;第二、提供静态函数loop(),使调用该函数线程进行无线循环,并从消息队列中读取消息。
第一点就是上面的sThreadLocal中的相关代码创建MessageQueue对象。
当需要一个线程变为异步消息处理线程的时候,在Thread类的run方法调用Looper.prepare()为该线程创建一个MessageQueue对象,在调用Looper.loop()函数,进行消息处理循环:
/**
* Run the message queue in this thread. Be sure to call
* {@link #quit()} to end the loop.
*/
public static void loop() {
final Looper me = myLooper();
if (me == null) {
throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
}
final MessageQueue queue = me.mQueue;
// Make sure the identity of this thread is that of the local process,
// and keep track of what that identity token actually is.
Binder.clearCallingIdentity();
final long ident = Binder.clearCallingIdentity();
for (;;) {
Message msg = queue.next(); // might block
if (msg == null) {
// No message indicates that the message queue is quitting.
return;
}
// This must be in a local variable, in case a UI event sets the logger
Printer logging = me.mLogging;
if (logging != null) {
logging.println(">>>>> Dispatching to " + msg.target + " " +
msg.callback + ": " + msg.what);
}
msg.target.dispatchMessage(msg);
if (logging != null) {
logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
}
// Make sure that during the course of dispatching the
// identity of the thread wasn't corrupted.
final long newIdent = Binder.clearCallingIdentity();
if (ident != newIdent) {
Log.wtf(TAG, "Thread identity changed from 0x"
+ Long.toHexString(ident) + " to 0x"
+ Long.toHexString(newIdent) + " while dispatching to "
+ msg.target.getClass().getName() + " "
+ msg.callback + " what=" + msg.what);
}
msg.recycleUnchecked();
}
}
该方法的解释:
Run the message queue in this thread. Be sure to call
就是返回线程中的消息队列,并回掉!
获得Looper对象
final
Looper me =
myLooper
();其实通过sThreadLocal()调用获得
/** * Return the Looper object associated with the current thread. Returns * null if the calling thread is not associated with a Looper. */ public static @Nullable Looper myLooper() { return sThreadLocal.get(); }
进入无线循环while(true):
for (;;) { Message msg = queue.next(); // might block if (msg == null) { // No message indicates that the message queue is quitting. return; }
可以看看next(),当message为空,则线程挂起。
msg.target.dispatchMessage(),完成消息的处理。
msg.target.dispatchMessage(msg);要看看里面的源码:/** * Handle system messages here. */ public void dispatchMessage(Message msg) { if (msg.callback != null) { handleCallback(msg); } else { if (mCallback != null) { if (mCallback.handleMessage(msg)) { return; } } handleMessage(msg); } }
处理完消息后ongmsg.recycleUnchecked();回收Message对象占用的系统资源。
MessageQueue消息队列:消息读/取不能同时进行,有枷锁机制
/**
* Low-level class holding the list of messages to be dispatched by a
* {@link Looper}. Messages are not added directly to a MessageQueue,
* but rather through {@link Handler} objects associated with the Looper.
*
* <p>You can retrieve the MessageQueue for the current thread with
* {@link Looper#myQueue() Looper.myQueue()}.
*/
public final class MessageQueue {
看源码:保存message的list【容器】,message不是直接添加道MessageQueue中取,通过looper对象来的。
Handler:
在Looper.looper()中取出消息后,回掉msg.target对象的handleMessage()添加函数,而msg.target的类型正是handler。
Handler处理消息地方:
/**
* Handle system messages here.
*/
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}
实际项目中的运用:三点要理解
第一种方式:
先:重写创建handler对象,重写handler的handler的handlerMessage()方法
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
//消息接收到了
int what = msg.what;
switch (what) {
case 1:
String text = (String) msg.obj;
ts.setText(text);
break;
case 2:
//刷新其他UI
break;
default:
break;
}
};
};
其次创建消息:
Message msg = new Message();//初始化方式
msg_ob.obj = "你家的牛在我菜园吃菜";
msg_ob.what = 1;//企业开发用常量
handler.sendMessage(msg);
下面是否封装的一个HandlerUtils工具:
public class MsgUtils {
static public void SendMSG(Handler handler,Object obj,int what,int arg1){
Message msg = new Message();
msg.obj = obj;
msg.what = what;
msg.arg1 = arg1;
handler.sendMessage(msg);
}
static public void SendMSG(Handler handler,Object obj,int what){
Message msg = new Message();
msg.obj = obj;
msg.what = what;
handler.sendMessage(msg);
}
static public void SendMSG(Handler handler,int what){
Message msg = new Message();
msg.what = what;
handler.sendMessage(msg);
}
static public void SendMSG(Handler handler,int what,int arg1){
Message msg = new Message();
msg.what = what;
msg.arg1 = arg1;
handler.sendMessage(msg);
}
static public void SendMSG(Handler handler,Object obj){
Message msg = new Message();
msg.obj = obj;
handler.sendMessage(msg);
}
static public void SendMSG(Handler handler,int what,Object obj,int arg1){
Message msg = new Message();
msg.obj = obj;
msg.arg1 = arg1;
msg.what = what;
handler.sendMessage(msg);
}
}
第二种方式:获取message消息方式不一样
Message msg_ob = handler.obtainMessage();//使用了池的概念
msg_ob.obj = "你家的牛在我菜园吃菜";
msg_ob.what = 1;//企业开发用常量
msg_ob.sendToTarget();
直接通过handler的obtainMessage()方法使用了池的概念
源码如下:
/**
* Returns a new {@link android.os.Message Message} from the global message pool. More efficient than
* creating and allocating new instances. The retrieved message has its handler set to this instance (Message.target == this).
* If you don't want that facility, just call Message.obtain() instead.
*/
public final Message obtainMessage()
{
return Message.obtain(this);
}
/**
* Same as {@link #obtainMessage()}, except that it also sets the what member of the returned Message.
*
* @param what Value to assign to the returned Message.what field.
* @return A Message from the global message pool.
*/
public final Message obtainMessage(int what)
{
return Message.obtain(this, what);
}
/**
* Same as {@link #obtain()}, but sets the value for the <em>target</em> member on the Message returned.
* @param h Handler to assign to the returned Message object's <em>target</em> member.
* @return A Message object from the global pool.
*/
public static Message obtain(Handler h) {
Message m = obtain();
m.target = h;
return m;
}
第三种方式,实现一个ruannable接口,然后将这个线程作为消息发送:
private Runnable run_handler = new Runnable() {
public void run() {
i++;
ts.setText("runnable 刷新UI"+i+"次");
handler.postDelayed(run_handler, 100);
}
};
这种方式的好处是随时添加删除:
handler.post(run_handler);
handler.removeCallbacks(run_handler);//停止循环
/**
* 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.
*
* @param r The Runnable that will be executed.
*
* @return 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.
*/
public final boolean post(Runnable r)
{
return sendMessageDelayed(getPostMessage(r), 0);
}
/**
* Causes the Runnable r to be added to the message queue, to be run
* at a specific time given by <var>uptimeMillis</var>.
* <b>The time-base is {@link android.os.SystemClock#uptimeMillis}.</b>
* Time spent in deep sleep will add an additional delay to execution.
* The runnable will be run on the thread to which this handler is attached.
*
* @param r The Runnable that will be executed.
* @param uptimeMillis The absolute time at which the callback should run,
* using the {@link android.os.SystemClock#uptimeMillis} time-base.
*
* @return 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. Note that a
* result of true does not mean the Runnable will be processed -- if
* the looper is quit before the delivery time of the message
* occurs then the message will be dropped.
*/
public final boolean postAtTime(Runnable r, long uptimeMillis)
{
return sendMessageAtTime(getPostMessage(r), uptimeMillis);
}