1. 程式人生 > >anr,多執行緒,handler

anr,多執行緒,handler

應該瞭解的知識:

1.什麼是程序、什麼是執行緒

2.為什麼要使用多執行緒

3.Handler機制

4.Handler原理

什麼是程序、什麼是執行緒

程序是一個程式的完全執行,包括了程式和程式需要的資源,是分配資源的基本單位;執行緒是獨立執行和獨立排程的基本單位,一邊聽歌一邊打字一邊玩遊戲看似是同時進行實際上是CPU執行時候,快速切換資源,讓使用者感覺是同時進行的,我們可以將執行緒理解為程式中的一段可執行的程式碼塊。

為什麼要使用多執行緒--》可以參考別人的專門理解的理解:http://blog.csdn.net/itjavawfc/article/details/50360776

 1)耗時的操作使用執行緒,提高應用程式響應
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();
    }
可以看到我們通過上面程式碼建立了Message物件。      上面的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);
        }
    }

處理完訊息後ong
msg.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);
    }