preface

  • Multithreaded applications are very common in Android development, and the common methods are:

    1. Thread class inheritance
    2. Implement the Runnable interface
    3. Handler
    4. HandlerThread
    5. AsyncTask
  • Today, I will present a comprehensive and detailed study guide to the HandlerThread mechanism, covering qualitative cognition, quantitative usage, how it works, and source code analysis. I hope you will enjoy it.


directory


Schematic diagram


Definition 1.

A lightweight asynchronous communication class that Android has packaged


2. The role

  1. Implement multithreading in the worker thread to perform tasks, such as time-consuming tasks
  2. Asynchronous communication, messaging

    Implement worker thread & main thread (UICommunication between threadsThat is, the result of the worker thread is passed to the main thread so that the relevant thread can be executed in the main threadUIoperation

This ensures thread safety


3. The advantages

Facilitate asynchronous communication, that is, the complex combination of “task threads (such as inheriting Thread classes) + Handler” is not required

In fact, handlerThreads essentially make it easier to create new threads and communicate with other threads by inheriting the Thread class and encapsulating the use of the Handler class


4. Working principle

Internal principle = Thread class + Handler class mechanism, namely:

  • Through inheritanceThreadClass,Quickly create a string withLooperObject’s new worker thread
  • By encapsulatingHandlerClass,Quickly createHandler& Communicates with other threads

5. Specific use

  • HandlerThreadThe essence of: inheritanceThreadClass & packagingHandlerclass
  • HandlerThreadThere are 5 steps to use
HandlerThread mHandlerThread = new HandlerThread(" HandlerThread "); // Step 2: Start the thread mhandlerThread.start (); // Step 3: Create a worker thread Handler & duplicate handleMessage () WorkHandler = new Handler(HandlerThread.getLooper ())  { @Override public boolean handleMessage(Message msg) { ... // Message processing return true; }}); // Step 4: Use the worker thread Handler to send a message to the worker thread's message queue // In the worker thread, when the message loops, fetch the corresponding message & perform related operations on the worker thread // a. Define Message MSG = message.obtain (); msg.what = 2; MSG. Obj = "B"; Workhandler. sendMessage(MSG); workhandler. sendMessage(MSG); // Step 5: Terminate the thread, that is, stop the thread's message loop mhandlerThread.quit ();Copy the code
  • For a more detailed explanation & sample application, see article: Android Multithreading: HandlerThread by Hand

6. Source code analysis

  • This source code analysis will be based onHandlerThreadTo explain how to use
  • Android Multithreading: this is a detailed source analysis of HandlerThread walkthrough

7. To summarize

  • This article gives a comprehensive introduction to multithreadingHandlerThread, including qualitative cognition, specific use, working principle, source code analysis and so on
  • And I’m going to continue with thatAndroidMultithreading related content, interested students can continue to pay attention to my operationsWechat Public Account:
  • I would like to introduce to you a unique Android wechat public account (benefits return)
  • I’d like to invite you to write Android with me

Thumb up, please! Because your encouragement is the biggest power that I write!

The Android Event distribution mechanism is the most comprehensive and easy-to-understand solution for Android screen adaptation. Android development: the most comprehensive, the most easy-to-understand Webview Android development: JSON introduction and the most comprehensive parsing method! BroadcastReceiver Is the most comprehensive version of Android’s BroadcastReceiver


Welcome to attentionCarson_HoJane books!

Share the dry things about Android development from time to time, the pursuit of short, flat, fast, but there is no lack of depth.