SoFunction
Updated on 2025-03-11

Android thread customization example of Looper with message loop

Android thread customization example of Looper with message loop

The UI thread of the Android system is a thread with a message loop (Looper) mechanism. At the same time, Android also provides a HandlerThread class encapsulated with a message loop (Looper). This thread can bind Handler() object and send messages to the thread through the Handler's sendMessage() function. Through the handleMessage() function, the messages received by the thread are processed. This is more abstract. Then, this article uses the basic Java class library to implement a thread with a message loop (Looper) to help beginners understand how such a Looper works.

1. First, we complete a simple threading framework. 

public class LooperThread {
   
  private volatile boolean mIsLooperQuit = false;
     
  private Thread mThread;   
   
  public void start() {    
    if( mThread != null ) {
      return;
    }   
    mIsLooperQuit = false;
    mThread = new Thread(mLooperRunnable);
    ();    
  }
   
  public void stop() {  
    if( mThread == null ) {
      return;
    }   
    mIsLooperQuit = true;
    mThread = null; 
  }
 
  protected Runnable mLooperRunnable = new Runnable() {  
 
    @Override
    public void run() {
      while( !mIsLooperQuit ) {
       
      }
    }
  };   
}

As shown in the above code, () loops to execute thread tasks, and mIsLooperQuit is the condition for thread to exit the loop. Below, we will add the sending and processing code for the message.

2. Add message sending and processing code for thread loops

(1) Define the message structure and create a message queue

public class LooperThread {
 
  private Queue<Message> mMessageQueue = new LinkedList<Message>();
   
  public static class Message {
    int what;
  }    
}

(2) Create mutexes and conditional variables

public class LooperThread {
 
   private Lock mLock = new ReentrantLock();
   private Condition mCondition = ();    
}

(3) Create a function to send messages

//Send a message, called by other external modules, send a message to the threadpublic void sendMessage( Message message ) {
  if( mThread == null ) {
    return;
  }   
  ();
  (message); //Add message to message queue  ();    //Notify the thread to loop. If there is a message coming, please deal with it immediately  ();
}

(4) Create a function that processes messages

//Processing messages, called internally by the threadpublic void handleMessage(Message message) {
  // Here you can call back the listener through a Callback}

(5) Parsing messages in a () loop

protected Runnable mLooperRunnable = new Runnable() {  
     
  @Override
  public void run() {
     
    while( !mIsLooperQuit ) {
     
      ();
      Message message = null;
     
      try {
        while( !mIsLooperQuit &amp;&amp; () ) {
          (); //Water sleep if there is no news.        } 
        message = ();         
      }
      catch (InterruptedException e) {
        ();      
      }
      finally {
        ();
      }   
       
      handleMessage(message );
    }          
  };   
}

(6) Modify the thread's Stop() function and wake up the sleeping message loop

public void stop() {  
 
  if( mThread == null ) {
    return;
  }   
 
  mIsLooperQuit = true;
     
  ();   
  ();
  ();
   
  ();
  mThread = null;   
}

At this point, a basic thread class encapsulation with a message loop is completed. I believe everyone should understand how the system implements a message loop from the process of writing this code.

If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!