安卓UI线程阻塞 [英] Android :UI Thread Blocked

查看:579
本文介绍了安卓UI线程阻塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序已创建了一个自定义对话框,这是双方的WebView和正常的Andr​​oid应用程序显示的,也是我做一些后台操作时,对话框显示,当过我叫mydialog功能它显示我的定制对话框,并且也返回一些值,当我使用的WebView + javainterface它是完美的工作,但它不与普通应用程序中工作时,流量为

首先,我会得到我的对话,我做了一些处理后(这里的主线程等待,对话需要显示,),那么我将返回字符串,问题是对话框不显示,当我调用此函数,而不是那该对话框将显示我的后台进程完成之后。

我把这叫做我的对话框如下:

 字符串样本= mydialog();公共字符串mydialog(){            字符串的myString = NULL;
                尝试{
                    myactivity.this.runOnUiThread(的ShowDialog);
                    而(customizeddialog.Getvalue()== NULL){                    }                    了mystring = customizeddialog.Getvalue();
                    customizeddialog.Setvalue(NULL);
                }赶上(例外五){            返回MyString的;        }        私人Runnable接口的ShowDialog =新的Runnable(){            公共无效的run(){
                    尝试{
                        customizeddialog m_dialog =新customizeddialog(myactivity.this);
                        m_dialog.setCancelable(假);
                        m_dialog.show();                    }赶上(例外五){                    }            }
        };


解决方案

当您输入 mydialog的同步块()你获得这个的锁。这里面同步块,你运行的ShowDialog()在UI线程上,并试图获得这个的锁时,会再次进入的ShowDialog 同步块>。

由于锁已经被收购了,它会等待,直到它在 mydialog释放(),这将永远不会发生,因为的ShowDialog 从来没有执行过去同步(本)。你有什么是死锁。

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.

这篇关于安卓UI线程阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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