简体   繁体   English

Android:UI线程被阻止

[英]Android :UI Thread Blocked

In my application I have created one customized dialog box ,which is showed in both webview and normal android application,and also I doing some background operation when the dialog box is showed, when ever I called the mydialog function it shows my customized dialog box and also it return some values,it is perfectly working when I use webview+javainterface but it doesn't work with ordinary applications, the flow is 在我的应用程序中,我创建了一个自定义对话框,该对话框同时显示在webview和普通android应用程序中,并且在显示该对话框时进行一些后台操作,每当我调用mydialog函数时,它都会显示我的自定义对话框,它也返回一些值,当我使用webview + javainterface时它可以正常工作,但不适用于普通应用程序,流程是

first I will get my dialog, after I do some process(here the main thread will wait ,dialog need to show,) then I will return the string ,the problem is dialog doesn't show when I called this function instead of that the dialog will showed after my background process finished. 首先,我得到对话框,经过一些处理(这里主线程将等待,需要显示对话框),然后返回字符串,问题是当我调用此函数而不是对话框时,对话框不显示我的后台进程完成后,将显示对话框。

I call this my dialog box like: 我将此称为我的对话框,例如:

String sample=mydialog();

public String mydialog() {

            String mystring = null;




                try {
                    myactivity.this.runOnUiThread(ShowDialog);


                    while (customizeddialog.Getvalue() == null) {

                    }

                    mystring = customizeddialog.Getvalue();
                    customizeddialog.Setvalue(null);
                } catch (Exception e) {



            return mystring;

        }

        private Runnable ShowDialog = new Runnable() {

            public void run() {


                    try {
                        customizeddialog m_dialog = new customizeddialog(myactivity.this);
                        m_dialog.setCancelable(false);
                        m_dialog.show();

                    } catch (Exception e) {

                    }



            }
        };

When you enter the synchronized block in mydialog() you acquire this 's lock. mydialog()输入synchronized块时,您将获得this锁。 Inside this synchronized block, you run ShowDialog() on the UI thread, and try to acquire this 's lock again when you enter the synchronized block in ShowDialog . 在此synchronized块内,在UI线程上运行ShowDialog() ,并在ShowDialog输入synchronized块时尝试再次获取this锁。

Since the lock has already been acquired, it will wait until it is released in mydialog() , which will never happen because ShowDialog never executes past synchronized(this) . 由于已经获得了锁,它将等待直到在mydialog()释放该mydialog() ,这将永远不会发生,因为ShowDialog从未执行过mydialog() synchronized(this) What you have is deadlock. 您所拥有的是僵局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM