Well, it's a question of logical order and concurrency. Try this way :
public void process() {
Thread funcAThread = new Thread(new Runnable() {
@Override
public void run() {
startFunctionA();
}
});
Thread funcBThread = new Thread(new Runnable() {
@Override
public void run() {
startFunctionB();
}
});
try {
funcAThread.start();
funcBThread.start();
funcAThread.join();
funcBThread.join();
startFunctionC();
startFunctionD();
} catch (InterruptedException e) {
Log.getStackTraceString(e);
}
}
Note: make sure that all call to UI Views (especially in function D) are processed inside the UI Thread using Activity.runOnUiThread(Runnable runnable)
.
Note 2: if functionC is time-consuming, you may call it from a Thread as well so that it keeps your app responsive (which is very important). So based on my code above :
try {
funcAThread.start();
funcBThread.start();
funcAThread.join();
funcBThread.join();
new Thread(new Runnable() {
@Override
public void run() {
startFunctionC();
//once completed, call UI thread for D !
runOnUiThread(new Runnable() {
@Override
public void run() {
startFunctionD();
}
});
}
}).start();
} catch (InterruptedException e) {
Log.getStackTraceString(e);
}
Note 3: I would not recommand the usage of AsyncTask
. Not because it's bad (certainly not) but because they are intended for operations where you need to define a beginning, a long operations to be processed in the background with regular updates performed on the UI thread and finally some other operations to be performed once the main long operations are completed.
You can use one AsyncTask to perform everything but you would loose the advantage of using two concurrent threads that can take advantage of multi-core CPUs and therefore process your data / perform your operations much faster. Furthermore, I believe on some Android version, you can only use one AsyncTask at the time. Check this answer for more informations : https://stackoverflow.com/a/4072832/3535408. So you are better off with threads : clearer, simplier and just as fast.