[Kernel Research] processor _ Handler, kernel research _ handler

Source: Internet
Author: User

[Kernel Research] processor _ Handler, kernel research _ handler

Although MessageQueue provides direct read/write function interfaces, it generally does not directly read/write message queues to programmers. The Handler get type is exactly 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();            }        }    }
Generally, the Handler class is used to send messages to the message queue, and the handleMessage () function of the Handler class is reloaded to add the Message Processing code.

The Handler object can only be added to a thread with a message queue; otherwise, an exception occurs. The following code is the constructor of the Handler class:

    /**     * 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;    }
Note the following code:

        mLooper = Looper.myLooper();        if (mLooper == null) {            throw new RuntimeException(                "Can't create handler inside thread that has not called Looper.prepare()");        }
From the code above, we can conclude that before constructing a Handler object, you must have executed logoff. prepare (), but prepare () cannot be executed twice. The following is the code for lorule. 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());    }
The Handler object can be created before or after the logoff. loop () function is executed.

In previous application development, Handler objects are generally added to the initialization code of the Activity. In fact, before the Activity object is constructed, the thread of the Activity has executed logoff. prepare () function. For details, see the following link.

Http://blog.csdn.net/manoel/article/details/39499747

A thread can contain multiple Handler objects. In the logoff. loop () function, different messages correspond to different Handler objects, and different handleMessage () functions are called back.




Q: Who has the handler information in java logs?

What information do you want?

The Handler object obtains the log information from the Logger and exports the information. For example, it can write the information to the console or file, send the information to the Network Log service, or forward it to the operating system log.

You can disable Handler by executing setLevel (Level. OFF) and re-enable it by executing the appropriate setLevel.

The Handler class usually uses the LogManager attribute to set the default values of the Filter, Formatter, and Level of the Handler.

Java. util. logging. Handler
Java. util. logging. MemoryHandler
Java. util. logging. StreamHandler
Java. util. logging. lelehandler
Java. util. logging. FileHandler
Java. util. logging. SocketHandler

The default log format is xml.

A good way to study Linux Kernel

Learning and studying the Linux kernel is a very difficult project and must be patient. I recommend several Linux kernel books. "a deep understanding of the Linux kernel" is a Linux kernel programming book. If you want to develop a driver, select "Linux Device Driver". if you are engaged in Application Programming in the UNIX/Linux environment, you can refer to "Advanced Programming in the UNIX environment".

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.