Android :UI Thread Blocked

2019-07-19 17:34发布

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

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) {

                    }



            }
        };

1条回答
手持菜刀,她持情操
2楼-- · 2019-07-19 18:14

When you enter the synchronized block in mydialog() you acquire this's lock. 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.

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). What you have is deadlock.

查看更多
登录 后发表回答