【核心研究】處理者_Handler,核心研究_handler
儘管MessageQueue提供了直接讀/寫的函數介面,但對於程式員來說,一般不直接讀/寫訊息佇列。之前瞭解到,在Looper.loop()函數中,當取出訊息後,會回調msg.target對象的handleMessage()函數,而msg.target的類型正是Handler。
/** * Run the message queue in this thread. Be sure to call * {@link #quit()} to end the loop. */ public static final void loop() { Looper me = myLooper(); MessageQueue queue = me.mQueue; while (true) { Message msg = queue.next(); // might block //if (!me.mRun) { // break; //} if (msg != null) { if (msg.target == null) { // No target is a magic identifier for the quit message. return; } if (me.mLogging!= null) me.mLogging.println( ">>>>> Dispatching to " + msg.target + " " + msg.callback + ": " + msg.what ); msg.target.dispatchMessage(msg); if (me.mLogging!= null) me.mLogging.println( "<<<<< Finished to " + msg.target + " " + msg.callback); msg.recycle(); } } }一般使用Handler類向訊息佇列中發送訊息,並重載Handler類的handleMessage()函數添加訊息處理代碼。
Handler對象只能添加到有訊息佇列的線程中,否則會發生異常。以下代碼是Handler類的建構函式:
/** * Default constructor associates this handler with the queue for the * current thread. * * If there isn't one, this handler won't be able to receive messages. */ public Handler() { if (FIND_POTENTIAL_LEAKS) { final Class<? extends Handler> klass = getClass(); if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) && (klass.getModifiers() & Modifier.STATIC) == 0) { Log.w(TAG, "The following Handler class should be static or leaks might occur: " + klass.getCanonicalName()); } } mLooper = Looper.myLooper(); if (mLooper == null) { throw new RuntimeException( "Can't create handler inside thread that has not called Looper.prepare()"); } mQueue = mLooper.mQueue; mCallback = null; }注意這句代碼:
mLooper = Looper.myLooper(); if (mLooper == null) { throw new RuntimeException( "Can't create handler inside thread that has not called Looper.prepare()"); }由以上代碼可以得出結論,在構造Handler對象前,必須已經執行過Looper.prepare(),但prepare()不能被執行兩次。下面是Looper.prepare()的代碼:
/** 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 final void prepare() { if (sThreadLocal.get() != null) { throw new RuntimeException("Only one Looper may be created per thread"); } sThreadLocal.set(new Looper()); }建立Handler對象可以在執行Looper.loop()函數之前,也可以在執行之後。
在以往的應用程式開發中,一般在Activity的初始化代碼中添加Handler對象,事實上,在Activity對象被構造前,Activity所在的線程已經執行了Looper.prepare()函數。具體可查看下面的連結。
http://blog.csdn.net/manoel/article/details/39499747
一個線程中可以包含多個Handler對象。在Looper.loop()函數中,不同的Message對應不同的Handler對象,從而回調不同的handleMessage()函數。
問,誰有java日誌中的處理者(handler)的相關資訊?
你想要什麼樣的資訊?
Handler 對象從 Logger 中擷取日誌資訊,並將這些資訊匯出。例如,它可將這些資訊寫入控制台或檔案中,也可以將這些資訊發送到部落格服務中,或將其轉寄到作業系統日誌中。
可通過執行 setLevel(Level.OFF) 來禁用 Handler,並可通過執行適當層級的 setLevel 來重新啟用。
Handler 類通常使用 LogManager 屬性來設定 Handler 的 Filter、Formatter 和 Level 的預設值。
java.util.logging.Handler
java.util.logging.MemoryHandler
java.util.logging.StreamHandler
java.util.logging.ConsoleHandler
java.util.logging.FileHandler
java.util.logging.SocketHandler
預設的日誌方式是xml格式
研究linux核心的好方法
學習和研究Linux核心是一個非常艱苦的工程,一定要有耐心。推薦幾本Linux核心的書籍,《深入理解Linux核心》這本是linux核心編程的,要是想做驅動,就選《Linux裝置驅動》,如果你是搞UNIX/Linux環境下的應用程式編程,那麼就看《UNIX環境進階編程》。