android studio main thread sends messages to child threads

1. Key code:

new Thread(new Runnable() {
    
    
            @SuppressLint("HandlerLeak")
            @Override
            public void run() {
    
    
                //run方法执行完,生命周期就结束了,我们主线程要给子线程发送消息,那么就要保证子线程不死
                /**
                 * 1.创建了Looper对象,然后Looper对象中创建了MessageQueue
                 * 2.并将当前的Looper对象跟当前的线程(子线程)绑定ThreadLocal
                 */
                Looper.prepare();//队列准备

                subHandler = new Handler(){
    
    
                    @Override
                    public void handleMessage(@NonNull Message msg) {
    
    
                        if (msg.what==2) {
    
    
                            Toast.makeText(MainActivity.this, "主线程:"+msg.obj.toString(), Toast.LENGTH_SHORT).show();
                        }
                    }
                };
                mLooper = Looper.myLooper();

                /**
                 * 1.从当前线程中找到之前创建的Looper对象,然后找到MessageQueue
                 * 2.开启死循环,遍历消息池中的消息
                 * 3.当获取到msg的时候,调用这个msg的handler的disPatchMsg方法,让msg执行起来
                 */
                Looper.loop();
                Log.d(TAG, "run: loop()执行完了");
            }
        }).start();

Send message code:

subHandler.obtainMessage(2,"你好,主线程,我是子线程").sendToTarget();

2. Process:

1. The life cycle of the child thread:

The run() method is finished, it means that the life cycle of the child thread ends

new Thread(new Runnable() {
    
    
            @SuppressLint("HandlerLeak")
            @Override
            public void run() {
    
    //这个就是run()方法
                
                Log.d(TAG, "run: loop()执行完了");
            }
        }).start();

2. Question:

As soon as the child thread is executed, the run() method will be executed, which means that the life cycle of the child thread ends directly. When it is over, how do we send messages?

3. Problem solving:

Keep child threads alive: how to do it?

  1. Create a queue,
/**
                 * 1.创建了Looper对象,然后Looper对象中创建了MessageQueue
                 * 2.并将当前的Looper对象跟当前的线程(子线程)绑定ThreadLocal
                 */
                Looper.prepare();//队列准备
  1. Execute endless loop
/**
                 * 1.从当前线程中找到之前创建的Looper对象,然后找到MessageQueue
                 * 2.开启死循环,遍历消息池中的消息
                 * 3.当获取到msg的时候,调用这个msg的handler的disPatchMsg方法,让msg执行起来
                 */
Looper.loop();

This way the child thread is kept alive
Insert picture description here

4. The main thread sends a message to the child thread

Create a Handler object in the loop of the child thread, waiting to receive the message

subHandler = new Handler(){
    
    
                    @Override
                    public void handleMessage(@NonNull Message msg) {
    
    
                        Log.d(TAG, "handleMessage: "+msg.toString());
                        Toast.makeText(MainActivity.this, "主线程:"+msg.obj.toString(), Toast.LENGTH_SHORT).show();
                    }
                };

The main thread performs message sending:

subHandler.obtainMessage(2,"你好,主线程,我是子线程").sendToTarget();

5. Finally, exit the queue, otherwise memory leaks

@Override
    protected void onDestroy() {
    
    
        super.onDestroy();
        if (mLooper!=null) {
    
    
            mLooper.quit();
        }
    }

Guess you like

Origin blog.csdn.net/sunweihao2019/article/details/109311728