我想启动 2 个异步任务,但只有一个被执行。LogCat 的输出应该是这样的:
firstAsync started
secondAsync started
secondAsync ends
firstAsync ends
end
但 LogCat 的输出是
firstAsync started
end
它表明从未执行过 secondAsync。这是java代码:
package com.example.async;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.app.Activity;
public class MainActivity extends Activity {
public boolean stopAsync=false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new firstAsync().execute();
new secondAsync().execute();
Log.e("end","end");
}
class firstAsync extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
Log.e("firstAsync started","firstAsync started");
while(!stopAsync)
{
}
Log.e("firstAsync ends","firstAsync ends");
return null;
}
}
class secondAsync extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
Log.e("secondAsync started","secondAsync started");
stopAsync = true;
Log.e("secondAsync ends","secondAsync ends");
return null;
}
}
}
所以,问题是,我怎样才能启动多个异步任务,希望你能帮助我。问候克里斯