为什么在doInBackground()中调用Thread.sleep会导致应用程序崩溃?

3

我正在使用AsyncTask从Firebase中获取数据,它可以工作。但是,我正在使用一个空的while循环来确保我的数据库完成操作,然后任务才会进入下一步。我知道在C语言中使用空的while循环会增加CPU使用率,并且通常是非常糟糕的实践。然而,当我尝试做我在C语言中会做的事情(调用sleep或wait)时,程序会崩溃。在安卓中有其他方法可以实现这一点吗?

protected String doInBackground(String... params) {

            // add a local list of the restaurants found at yelp and a reference for the background thread to process.
            addRes(yelp.getRestaurants());

            // loop through each restaurant and add all the menu items from each one. I think we need to randomize this list later on to provide variety in the app.
            for (int i = 0; i < restNames.size(); i++) {

            firebase = new firebaseHandler("Tastr Items/" + restNames.get(i) + "/Menu");
            System.err.println("Adding Menu Item From ----> " + restNames.get(i));
            firebase.readFromDatabase();

            // Wait for firebase to finish adding new data, I would like to call sleep() here but it crashes the app for some reason.
            while (!firebase.isReaderDone()) {

                // Here his where I want to call sleep until firebase Handler is finished.
                try {
                    wait(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            // second loop to run through each menu item at each restaurant, this would be very inefficient for a huge number, but for now it should be fine if we have less than 100 or so menu items per restaurant.
            for (int j = 0; j < firebase.getReaderList().size(); j++) {

            System.err.println("Adding Menu Item ----> " + firebase.getReaderList().get(j));
            addMenuItem(firebase.getReaderList().get(j));
            firebase.changeReference("Tastr Items/" + restNames.get(i) + "/Menu/" + firebase.getReaderList().get(j) + "/Image Path");

            }
        }

        return null;
    }//doInBackground

这里是错误信息

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
              Process: com.smileyface.tastr, PID: 5863
              java.lang.RuntimeException: An error occurred while executing doInBackground()
                  at android.os.AsyncTask$3.done(AsyncTask.java:325)
                  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                  at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                  at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                  at java.lang.Thread.run(Thread.java:761)
               Caused by: java.lang.IllegalMonitorStateException: object not locked by thread before wait()
                  at java.lang.Object.wait(Native Method)
                  at com.smileyface.tastr.Activities.TouchActivity$dataLoader.doInBackground(TouchActivity.java:369)
                  at com.smileyface.tastr.Activities.TouchActivity$dataLoader.doInBackground(TouchActivity.java:346)
                  at android.os.AsyncTask$2.call(AsyncTask.java:305)
                  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
                  at java.lang.Thread.run(Thread.java:761

1
你能否发布崩溃的堆栈跟踪信息? - Raghunandan
1
请查看此链接:https://dev59.com/HV8d5IYBdhLWcg3wpzhf - Tasneem
1
在stackoverflow上快速搜索https://dev59.com/SnI_5IYBdhLWcg3wEe1u - Raghunandan
1
你不能在异步任务中使用线程,所以你可以使用runUi来解决这个问题。 - Vadivel
1
小提示:堆栈跟踪中以“Caused by:”开头的行是您崩溃的真正原因,通过在Google中搜索该行可以很快找到答案。 - Sunil Sunny
显示剩余2条评论
1个回答

2
使用倒计时器
 CountDownTimer test=  new CountDownTimer(3000,1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            // do when countdown timer is started like show progressbar

        }

        @Override
        public void onFinish() {

            //write what ever you want to do after completing time like hiding progress bar
        }
    };


    test.start();

这里的1000是开始时间,3000是以毫秒为单位的结束时间。


2
这会降低 CPU 使用率吗?能否解释一下原因?谢谢! - Remixt
1
doinbackground方法通常在单独的线程(后台线程)上运行,这可能会导致问题。 - Manohar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接