简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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