To make this question clear in depth, we need to answer it from multiple angles

What is a UI thread

The zygote process, the core Android process, forks out our app. When the app starts, it eventually goes to the Main method in the ActivityThread, which calls Looper. The thread where the ActivityThread resides is called the UI thread, or the Main thread. ActivityThread Main thread: ActivityThread

public static void main(String[] args) { ... . // Comment 1 looper.loop (); throw new RuntimeException("Main thread loop unexpectedly exited");
    }
Copy the code

Note 1: Just look at the last two lines, in loop.loop (); After the call, an exception is thrown, which refers to the current thread as Main Thread.

How UI threads work

To understand how UI threads work, you need to understand Android’s messaging mechanism. A simple picture can illustrate this concept, as shown below

Why can’t the UI thread update

Example of building a child thread to update the UI

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showTv = findViewById(R.id.show_tv);
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                showTv.setText("Child thread displays UI");
            }
        }.start();
    }
Copy the code

An exception is thrown after the run

   android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
        at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7957)
        at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1274)
        at android.view.View.requestLayout(View.java:23120)
        at android.view.View.requestLayout(View.java:23120)
        at android.view.View.requestLayout(View.java:23120)
        at android.view.View.requestLayout(View.java:23120)
        at android.view.View.requestLayout(View.java:23120)
        at android.view.View.requestLayout(View.java:23120)
        at androidx.constraintlayout.widget.ConstraintLayout.requestLayout(ConstraintLayout.java:3172)
        at android.view.View.requestLayout(View.java:23120)
        at android.widget.TextView.checkForRelayout(TextView.java:8914)
        at android.widget.TextView.setText(TextView.java:5736)
        at android.widget.TextView.setText(TextView.java:5577)
        at android.widget.TextView.setText(TextView.java:5534)
        at com.example.myapplication.MainActivityThe $1.run(MainActivity.java:29)
Copy the code

Here we add thread. sleep to the child Thread to do some time-consuming operations

The raised exception information can be located to the corresponding source viewrootimpl.java

    void checkThread() {
        if(mThread ! = Thread.currentThread()) { throw new CalledFromWrongThreadException("Only the original thread that created a view hierarchy can touch its views.");
        }
    }
    
    @Override
    public void requestLayout() {
        if(! 2 checkThread mHandlingLayoutInLayoutRequest) {/ / comment (); mLayoutRequested =true; scheduleTraversals(); }}Copy the code

Note 2: As you can see from the code, part of the exception thrown is called by requestLayout(), which is one of the methods called if you know how the view is drawn.

Thread.sleep() was added to the sample code to perform the elapsed operation. Without this elapsed operation, the setText UI update operation would be performed normally. There needs to be further explore mHandlingLayoutInLayoutRequest the Boolean variable time could be changed. Thread.currentthread () is not changed when mThread is initially set to Thread. A little more exploring ideas can better read the source code, targeted.

Does Android provide controls that update the UI without a UI thread

This question, we need to explore the implementation of SufaceView related class, we can get the answer we want.

What if a non-UI thread wants to update the UI

Handler Method of sending messages

postInvalidate()

Either way we can update the UI indirectly

thinking

A simple problem, in fact, there are a lot of content to start with, so much content need to constantly look at the source code found summary to obtain.