为什么不能线程不是UI线程访问有何看法? [英] Why cant a thread that is not the UI thread access the view?

查看:198
本文介绍了为什么不能线程不是UI线程访问有何看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,除非是在UI线程没有线程可以访问当前视图。我想知道为什么吗?为何重要哪个线程正在改变看法?它是一个安全的理由吗?这是周围的工作,我用:

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;'

这是一种痛苦要做到这一点,每次我想改变布局。是否有不同的变通?我知道异步任务,我从来没有发现使用它的好方法,它比我在做什么好?所有培训相关答案appriciated!

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!

推荐答案

是你的权利:不能修改另一个线程安全起见看法(这就是为什么它被称为UI线程)。它prevents你离开的UI数据,这可能会引起程序崩溃,并会真的很难调试不一致的状态。所以Android的API只是禁止了(这是一个好主意)。这是你将在大多数API找到一个共同的UI模式。

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.

您可以更新任何视图<一个href=\"http://developer.android.com/reference/android/view/View.html#post%28java.lang.Runnable%29\">post()或<一个href=\"http://developer.android.com/reference/android/app/Activity.html#runOnUiThread%28java.lang.Runnable%29\">runOnUiThread():

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

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


为什么这个模式?的结果
同步不是免费的。它影响了性能。因此,它更容易保持修改在同一线程上的UI。


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.

如果我能修改来自不同线程的数据会发生什么?的结果
例如:螺纹 A 正在改变视图和线程的颜色 B 正在读同一添色。由于在执行多线程不保证该指令首次你可能会得到意想不到的效果。颜色是黑色的( 0 | 0 | 0 )之前,螺纹 A 要设置白色( 255 | 255 | 255 ),并用红色成分设置为 255 ,螺纹 B 开始阅读和线程之前获得了整个色 A 有机会来完成,并得到了红色( 255 | 0 | 0 ),而不是黑色的。

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.

这是一个简单的例子,这将影响到视觉效果,但如果这种情况发生了一些非常重要的数据应用程序将崩溃可怕和这样的错误是如此肮脏和难以调试。有很多了解多线程,也许本Java教程是一个很好的起点..

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...

这篇关于为什么不能线程不是UI线程访问有何看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆