As I see therethey are two independent methods, so there's no problem in parallelizing them. You could launch an executor service to do that and then await for termination:
@Override
protected Integer doInBackground(Void... params) {
Integer result = null;
try{
ExecutorService executor = Executors.newFixedThreadPool(2);
Runnable thread1 = new Runnable() {
@Override
public void run() {
try {
method1(_phoneno);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
};
executor.submit(thread1);
Runnable thread2 = new Runnable() {
@Override
public void run() {
try {
result = method2(_phoneno,_ticket);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
};
executor.submit(thread2);
executor.shutdown();
//Timeout for both tasks = 120 seconds
if (!executor.awaitTermination(120, TimeUnit.SECONDS)){
throw new RuntimeException("Could not complete requests in the given time!");
}
return result;
}catch(Exception ex){
//Keep your exception to be shown in a toast
exception = ex;
}
}
See also: