我有一个Android应用程序,我正在做以下事情:
private void onCreate() {
final ProgressDialog dialog = ProgressDialog.show(this, "Please wait..", "Doing stuff..", true);
new Thread() {
public void run() {
//do some serious stuff...
dialog.dismiss();
}
}.start();
stepTwo();
}
我想确保我的线程在stepTwo()之前完成;叫做.我怎样才能做到这一点?
谢谢!
解决方法:
Thread实例有一个join方法,所以:
private void onCreate() {
final ProgressDialog dialog = ProgressDialog.show(this, "Please wait..", "Doing stuff..", true);
Thread t = new Thread() {
public void run() {
//do some serious stuff...
dialog.dismiss();
}
};
t.start();
t.join();
stepTwo();
}
你可能想试试这个:
private void onCreate() {
final ProgressDialog dialog = ProgressDialog.show(this, "Please wait..", "Doing stuff..", true);
Thread t = new Thread() {
public void run() {
//do some serious stuff...
SwingUtilities,invokeLater(new Runnable() {
public void run() {
dialog.dismiss();
}
});
stepTwo();
}
};
t.start();
}
因为onCreate在UI线程中,所以在那里加入会冻结UI直到onCreate完成之后,保存任何对话框直到那时. stepTwo必须使用SwingUtilities.invokeLater自行进行任何UI更改.
标签:java,android,multithreading
来源: https://codeday.me/bug/20190606/1190795.html