我有这种情况:
3种公共方法,其中2种是命令,另外1种是检查输入,它们将在线程中启动command1或command2。我的问题是我该怎么做?
public void blablabla() {
input = get_user_input;
if(input == 1) {
start thread with command1
} else {
start thread with command2
}
}
public void command1() {
// do action
}
public void command2() {
// do action2
}
我认为它不可能直接使用方法启动线程,但是使用Runnable我可以做到这一点。我在想它的好选择和性能的好主意是声明command1和command2在类上是可静态运行的,并使用此可运行变量启动线程。将此操作作为静态可运行程序将比方法花费更多的内存?
谢谢!
您可以使用Asyn线程。试试下面的例子
private class GetExampleDetails extends AsyncTask<String, Void, Void> {
private ProgressDialog dialog = new ProgressDialog(ExpensesListView.this);
String strMsg = null;
protected void onPreExecute() {
this.dialog.setMessage("Loading.....");
this.dialog.setCanceledOnTouchOutside(false);
this.dialog.show();
}
// automatically done on worker thread (separate from UI thread)
protected Void doInBackground(final String... a) {
try
{
//Write your code. which one do backround
}
catch(Exception ex)
{
ex.printStackTrace();
}
return null;
}
// can use UI thread here
protected void onPostExecute(final Void unused) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
this.dialog = null;
}
}
}//Endtask
检查这一。我认为这对您有帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句