I’m trying to run two AsyncTasks at the same time. (Platform is Android 1.5, HTC Hero.)
However, only the first gets executed. Here’s a simple snippet to describe my problem:
public class AndroidJunk extends Activity {
class PrinterTask extends AsyncTask<String, Void, Void> {
protected Void doInBackground(String ... x) {
while (true) {
System.out.println(x[0]);
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new PrinterTask().execute("bar bar bar");
new PrinterTask().execute("foo foo foo");
System.out.println("onCreate() is done.");
}
}
The output I expect is:
onCreate() is done.
bar bar bar
foo foo foo
bar bar bar
foo foo foo
And so on. However, what I get is:
onCreate() is done.
bar bar bar
bar bar bar
bar bar bar
The second AsyncTask never gets executed. If I change the order of the execute() statements, only the foo task will produce output.
Am I missing something obvious here and/or doing something stupid? Is it not possible to run two AsyncTasks at the same time?
Edit: I realized the phone in question runs Android 1.5, I updated the problem descr. accordingly. I don’t have this problem with an HTC Hero running Android 2.1. Hmmm …