gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 09:39:24 25 4
gpt4 key购买 nike

我正在使用 AsyncTask 从我的 firebase 中提取数据并且它有效,但是我正在使用一个空的 while 循环来确保我的数据库在任务进入下一步之前完成它的操作。我知道在 c 中使用空的 while 循环会增加 CPU 使用率,这通常是非常糟糕的做法。但是,当我尝试执行我在 c 中执行的操作(调用 sleep 或等待)时,程序崩溃了。还有其他方法可以为 Android 执行此操作吗?

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) 

最佳答案

使用倒计时

 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 是结束时间,以毫秒为单位

关于android - 为什么在 doInBackground() 中调用 Thread.sleep 会使应用程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40756637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com