简体   繁体   中英

What's the relationship between java thread and system thread

Recently, I researched the Handler class in Android System. In my opinion, the Handler mechanism is that the Thread runs a dead loop and retrieves messages from the queue repeatedly in that dead loop, it then sends the message to target handler.

But when there aren't message in queue, the thread have to be waited or blocked for a specified time, this can reduce CPU time. My understanding is that in order to wait or block the Thread for a specified time, it uses the linux epoll function to wait specified time in native layer. It then uses a linux pipe to wake the thread when there are messages in queue.

So my confusion is why Android systems use Linux process communication functions(IPC) to control the Java Thread to wait or wake up? And what's the relationship between Java Thread and system thread or linux thread ?

In other words,what I really want to know is why android use linux ipc function to control the java threads to implement so-called Handler which is used to send message between java threads.

Here is the relevant code from the Android platform

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.recycle();
        }
    }

After reading your question, my curiosity lead me to this . And i believe there is a misunderstanding about the use of Linux process communication functions(IPC) to control the Java Thread .

I don't believe that i can explain it any better that the beautiful description given in the link.

Communication Mechanism of Android Processes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM