Why cant a thread that is not the UI thread access the view?

人走茶凉 提交于 2019-12-02 04:06:52

问题


I know that no thread can access the current view unless it is the UI Thread. I am wondering why? Why does it matter which thread is changing the view? Is it a security reason? This is the work around I use:

        public void doLayout()
        {
            Runnable run = new Runnable()
            {
                public void run()
                {
                    ViewerActivity.setContentView(R.layout.main);
                }
            };

            handler.post(run);
        }

        private Handler handler;'

It is kind of a pain to do that everytime i want to change the layout. Is there a different work around? I know about async task and i never found a good way to use it, is it better than what i'm doing? All relevent answers are appriciated!


回答1:


Yes your right: You can't modify views on another thread for the sake of security (that's why it's called UI thread). It prevents you from leaving the UI data in an inconsistent state which might crash your application and would be really hard to debug. So the android API simply forbids that (and that's a good idea). That's a common UI pattern which you'll find in the most APIs.

You can update any view with post() or runOnUiThread():

anyView.post(new Runnable() {
    public void run() {
        // do update here
    }
});

Why this pattern?
Synchronization is not for free. It impacts the performance. So it's easier to keep modifications to UI on the same thread.

If I could modify data from different threads what could happen?
For example: Thread A is changing the color of a view and thread B is reading the color at the same tim. Since multi-threading doesn't guarantee which instruction is executed first you might get unexpected results. The color is black (0|0|0) before, thread A wants to set white (255|255|255) and start with setting the red component to 255, thread B starts to read and gets the whole color before thread A had a chance to finish and got the color red (255|0|0) instead black.

This is a simple example which would impact a visual effect but if that happens for some really important data your application will crash horribly and such an error is so nasty and hard to debug. There's a lot to learn about multi-threading and maybe this java tutorial is a good starting point...




回答2:


Android application uses Single Thread Model and this thread is responsible to dispatch various events to the UI elements. This single thread model has two rules :

  1. Do not block the UI thread
  2. Do not access the Android UI toolkit from outside the UI thread

If you create a thread that uses View outside the UI thread then it violates the second rules.




回答3:


Security isn't the only UI thread is the only thread that can access the Views. The main reason is that the code behind the views probably isn't thread safe. This means that there are no guarantees that data won't get corrupted if you have multiple threads reading and writing to common variables.

Check out this great wikipedia article about thread safety.



来源:https://stackoverflow.com/questions/7700881/why-cant-a-thread-that-is-not-the-ui-thread-access-the-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!